【问题标题】:Using ng-blur in custom directive在自定义指令中使用 ng-blur
【发布时间】:2015-11-19 08:10:10
【问题描述】:

我在自定义指令中使用 ng-blur 时遇到问题。我想要的是能够制作一个可以处理发送到指令上ng-blur 属性的任何类型函数的组件。

这是指令示例:

<az-dir ng-blur="change()" lid="test" ng-model="obj.test"></az-dir>

Javascript 指令

app.directive('azDir', azDir);
function azDir() {
  return {
    restrict: 'E',
    scope: {
      ngModel: '=',
      ngBlur: '=',
      lid: '@'
    },
    templateUrl: 'directive.html',
    replace: true,
    require: 'ngModel'
  };
}

简单的角度控制器:

var app = angular.module('ashtest', []);

app.controller('TopCtrl', ['$scope',
  function($scope) {

    $scope.obj = {
      test: "Ashkan"
    };

    $scope.change = function() {
      $scope.obj.test = "changedThis";
    }


  }
]);

My Plunker Sample

【问题讨论】:

    标签: angularjs angular-directive


    【解决方案1】:

    ngBlur: '&',

    解释:

    1. “@”(文本绑定/单向绑定)
    2. "="(直接模型绑定/双向绑定)
    3. “&”(行为绑定/方法绑定)

    What is the difference between '@' and '=' in directive scope in AngularJS?

    【讨论】:

    • 在我的 plunker 上试试,如果你使用 '&' 它不会触发
    • 这样对我有用
    • 太棒了! ng-blur="ngBlur()" 这就是我所缺少的。谢谢! :)
    猜你喜欢
    • 2012-12-14
    • 1970-01-01
    • 2016-08-25
    • 2017-01-02
    • 2013-07-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多