在引用的调用控制器功能Angularjs元素 [英] Referencing the element that's calling a controller function Angularjs

查看:216
本文介绍了在引用的调用控制器功能Angularjs元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

我意识到我的意思是更抽象的比我,所以我创建了一个<一个href=\"http://stackoverflow.com/questions/24616524/referencing-the-element-that-is-calling-a-controller-function-angularjs-ng-cha\">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);
};

这篇关于在引用的调用控制器功能Angularjs元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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