引用调用控制器函数 Angularjs 的元素( ng-change/ng-blur/ng-* ? ) [英] Referencing the element that is calling a controller function Angularjs ( ng-change / ng-blur / ng-* ? )

查看:25
本文介绍了引用调用控制器函数 Angularjs 的元素( ng-change/ng-blur/ng-* ? )的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

原始问题询问如何确定哪个元素调用了控制器 blurr 函数,但我没有说明我并不是专门询问 ng-blur,而是 ng-* (ng-更改ng-focusng-mouseover、ng-*).所以,考虑到这一点:

如何确定哪个元素输入正在调用 blurr() 和/或 check() 函数?

html

<div ng-controller="Cntrlr 作为 cntrlr"><form name="meta_test"><input type="text" name='inpt' ng-model="cntrlr.inpt" ng-blur="cntrlr.blurr()" ng-change="cntrlr.check()"/><input type="text" name='second' ng-model="cntrlr.second" ng-blur="cntrlr.blurr()" ng-change="cntrlr.check()"/></表单>

js

var app = angular.module("test", []);app.controller("Cntrlr", ["$scope", function($scope){this.blurr = 函数(){alert("我是哪个输入?");alert("这太元了.");//?};this.check = 函数(){alert("这就是我的元数据:");警报(这个);}$scope.Cntrlr = 这个;//见:(参考)返回 $scope.Cntrlr;}]);

您可能会问自己他为什么要这样做?"
有两个原因:

  1. 因为我想打电话:

    $scope.user_form[meta_test.[(whatever this element is.name)]].$setValidity('spike', false);

  2. 因为我很好奇.必须有一个简单的方法来做到这一点.

(参考):控制器语法

解决方案

使用这个 -

<input type="text" name='inpt' ng-model="cntrlr.inpt" ng-blur="cntrlr.blurr($event)" ng-change="cntrlr.check()"/>

这将返回导致模糊功能的事件的 jQuery lite 版本.一旦您在控制器中收到这个元素,您几乎可以用它做任何您想做的事情.

事件的 .target 属性将为您提供所需的元素.

应该可以

The original question asked about how to determine which element called the controllers blurr function, but I didn't clarify that I was not specifically asking about ng-blur, but ng-* (ng-change, ng-focus, ng-mouseover, ng-*) in general. So, with that in mind:

How do I determine which element input is calling the blurr() and/or check() functions?

html

<body ng-app="test">
  <div ng-controller="Cntrlr as cntrlr">
    <form name="meta_test">
      <input type="text" name='inpt' ng-model="cntrlr.inpt" ng-blur="cntrlr.blurr()" ng-change="cntrlr.check()" />
      <input type="text" name='second' ng-model="cntrlr.second" ng-blur="cntrlr.blurr()" ng-change="cntrlr.check()" />
    </form>
  </div>
</body>

js

var app = angular.module("test", []);
app.controller("Cntrlr", ["$scope", function($scope){
  this.blurr = function(){
    alert("which input am I?");
    alert("this is so meta.");
    // ?
  };
  this.check = function(){
    alert("this is how meta I am:");
    alert(this); 

  }
 $scope.Cntrlr = this;  // see: (reference)
 return $scope.Cntrlr; 
}]);

You may be asking yourself "why would he want to do this?"
There are 2 reasons:

  1. because I want to call:

    $scope.user_form[meta_test.[(whatever this element is.name)]].$setValidity('spike', false);

  2. because I'm curious. There has to be a simple way to do this.

(reference): controller as syntax

解决方案

Use this -

<input type="text" name='inpt' ng-model="cntrlr.inpt" ng-blur="cntrlr.blurr($event)" ng-change="cntrlr.check()" />

This returns the jQuery lite version of the event that causes the blurr function. Once you receive this element in your controller, you can pretty much do whatever you want with it.

The .target attribute of the event will give you the required element.

Should work

这篇关于引用调用控制器函数 Angularjs 的元素( ng-change/ng-blur/ng-* ? )的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆