Referencing the element that's calling a contr

2020-08-05 11:14发布

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?

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.");
    // ?
  };
}]);

[edit]

I realized I meant to be more abstract than I was so I created a new question because this one has been solved

1条回答
虎瘦雄心在
2楼-- · 2020-08-05 11:42

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)" />

And

$scope.blurr = function(event){
    var $this = $(event.target);
    console.log($this);
};
查看更多
登录 后发表回答