在引用的调用控制器功能Angularjs元素控制器、元素、功能、Angularjs

2023-09-14 23:06:39 作者:岁月并非如歌

相当简单,但我想不出我需要谷歌的词。我如何引用任何元素调用控制器罗嗦功能?

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>

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问题因为这一个已经解决了

推荐答案

您可以通过 $事件的功能,并从找出事件的目标

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);
};