引用正在调用一个控制器功能Angularjs元素(NG-改变/ NG-模糊/ NG- *?) [英] Referencing the element that is calling a controller function Angularjs ( ng-change / ng-blur / ng-* ? )

查看:208
本文介绍了引用正在调用一个控制器功能Angularjs元素(NG-改变/ NG-模糊/ NG- *?)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

<一个href=\"http://stackoverflow.com/questions/24615597/referencing-the-element-thats-calling-a-controller-function-angularjs\">The原来的问题被问及如何确定哪些元素称为控制器罗嗦的功能,但我并没有澄清,我没有特别问 NG-模糊,但NG- *( NG-变化 NG-焦 NG-鼠标悬停,NG- *)一般。因此,考虑到这一点:

The original question asked about how to determine which element called the controllers blurr function, but I didn't clarify that I was not specifically asking about ng-blur, but ng-* (ng-change, ng-focus, ng-mouseover, ng-*) in general. So, with that in mind:

<body ng-app="test">
  <div ng-controller="Cntrlr as cntrlr">
    <form name="meta_test">
      <input type="text" name='inpt' ng-model="cntrlr.inpt" ng-blur="cntrlr.blurr()" ng-change="cntrlr.check()" />
      <input type="text" name='second' ng-model="cntrlr.second" ng-blur="cntrlr.blurr()" ng-change="cntrlr.check()" />
    </form>
  </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.");
    // ?
  };
  this.check = function(){
    alert("this is how meta I am:");
    alert(this); 

  }
 $scope.Cntrlr = this;  // see: (reference)
 return $scope.Cntrlr; 
}]);

您可能会问自己:他为什么要这么做?结果
有2个原因:

You may be asking yourself "why would he want to do this?"
There are 2 reasons:


  1. 因为我想打电话:

  1. because I want to call:

$ scope.user_form [meta_test [(无论这个元素is.name)]] $ setValidity('秒杀',FALSE);

因为我很好奇。必须有一个简单的方法来做到这一点。

because I'm curious. There has to be a simple way to do this.

(参考):
控制器作为语法

推荐答案

使用此 -

<input type="text" name='inpt' ng-model="cntrlr.inpt" ng-blur="cntrlr.blurr($event)" ng-change="cntrlr.check()" />

这将返回导致罗嗦函数的事件的jQuery的精简版的版本。一旦你在你的控制器收到此元素,可以pretty很多做任何你想用它。

This returns the jQuery lite version of the event that causes the blurr function. Once you receive this element in your controller, you can pretty much do whatever you want with it.

本次活动的.TARGET属性会给你所需的元素。

The .target attribute of the event will give you the required element.

应工作

这篇关于引用正在调用一个控制器功能Angularjs元素(NG-改变/ NG-模糊/ NG- *?)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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