相当简单,但我想不出我需要谷歌的词。我如何引用任何元素调用控制器罗嗦功能?
Fairly simple but I can't figure out the term I need to google. How do I reference whichever element is calling the controllers blurr function?
<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>
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问题因为这一个已经解决了
您可以通过 $事件
的功能,并从找出事件的目标
You could pass the $event
to the function, and figure out the target of the event from that:
<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);
};