引用调用控制器函数Angularjs的元素

时间:2014-07-07 16:41:01

标签: angularjs angularjs-scope angularjs-controller angularjs-model

相当简单,但我无法弄清楚我需要谷歌的术语。 如何引用调用控制器模糊函数的任何元素?

的HTML

<body ng-app="test">
  <div ng-controller="Cntrlr as cntrlr">
    <input type="text" ng-model="cntrlr.inpt" ng-blur="cntrlr.blurr()" />
    <input type="text" ng-model="cntrlr.second" ng-blur="cntrlr.blurr()" />
  </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.");
    // ?
  };
}]);

[编辑]

我意识到我的意思比我更抽象,所以我创建了new question,因为这个问题已经解决了

1 个答案:

答案 0 :(得分:2)

您可以将$event传递给该函数,并从中找出事件的目标:

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

$scope.blurr = function(event){
    var $this = $(event.target);
    console.log($this);
};