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

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

问题描述

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

html

<div ng-controller="Cntrlr 作为 cntrlr"><input type="text" ng-model="cntrlr.inpt" ng-blur="cntrlr.blurr()"/><input type="text" ng-model="cntrlr.second" ng-blur="cntrlr.blurr()"/>

js

var app = angular.module("test", []);app.controller("Cntrlr", ["$scope", function($scope){this.blurr = 函数(){alert("我是哪个输入?");alert("这太元了.");//?};}]);

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

解决方案

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

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

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

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

解决方案

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

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

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