AngularJS - 属性指令输入值更改

Mik*_*ras 40 jquery attributes directive angularjs

我有一个AngularJS属性指令,我想在父输入的值改变的任何时候采取行动.现在我正在使用jQuery:

angular.module("myDirective", [])
.directive("myDirective", function()
{
    return {
        restrict: "A",
        scope:
        {
            myDirective: "=myDirective"
        },
        link: function(scope, element, attrs)
        {
            element.keypress(function()
            {
                // do stuff
            });
        }
    };
});
Run Code Online (Sandbox Code Playgroud)

有没有办法在没有jQuery的情况下做到这一点?我发现keyPress事件并没有完全符合我的要求,虽然我确信我会想出一个解决方案,但当我在Angular项目中使用jQuery时会感到有些紧张.

那么Angular的做法是什么?

Lan*_*don 64

AngularJS文档中有一个很好的例子.

这是非常好的评论,应该让你指向正确的方向.

一个简单的例子,也许更多你正在寻找的是下面的:

的jsfiddle


HTML

<div ng-app="myDirective" ng-controller="x">
    <input type="text" ng-model="test" my-directive>
</div>
Run Code Online (Sandbox Code Playgroud)

JavaScript的

angular.module('myDirective', [])
    .directive('myDirective', function () {
    return {
        restrict: 'A',
        link: function (scope, element, attrs) {
            scope.$watch(attrs.ngModel, function (v) {
                console.log('value changed, new value is: ' + v);
            });
        }
    };
});

function x($scope) {
    $scope.test = 'value here';
}
Run Code Online (Sandbox Code Playgroud)

编辑:同样的事情,不需要ngModel jsfiddle:

JavaScript的

angular.module('myDirective', [])
    .directive('myDirective', function () {
    return {
        restrict: 'A',
        scope: {
            myDirective: '='
        },
        link: function (scope, element, attrs) {
            // set the initial value of the textbox
            element.val(scope.myDirective);
            element.data('old-value', scope.myDirective);

            // detect outside changes and update our input
            scope.$watch('myDirective', function (val) {
                element.val(scope.myDirective);
            });

            // on blur, update the value in scope
            element.bind('propertychange keyup paste', function (blurEvent) {
                if (element.data('old-value') != element.val()) {
                    console.log('value changed, new value is: ' + element.val());
                    scope.$apply(function () {
                        scope.myDirective = element.val();
                        element.data('old-value', element.val());
                    });
                }
            });
        }
    };
});

function x($scope) {
    $scope.test = 'value here';
}
Run Code Online (Sandbox Code Playgroud)

  • 如果您希望该指令充当`ngModel`,您可以使用具有双向绑定的隔离范围,如下所示:http://jsfiddle.net/langdonx/djtQR/ (3认同)

Jmr*_*Jmr 12

由于这必须有一个输入元素作为父元素,你可以使用

<input type="text" ng-model="foo" ng-change="myOnChangeFunction()">
Run Code Online (Sandbox Code Playgroud)

或者,您可以使用ngModelController并添加一个函数$formatters,该函数在输入更改时执行函数.见http://docs.angularjs.org/api/ng.directive:ngModel.NgModelController

.directive("myDirective", function() {
  return {
    restrict: 'A',
    require: 'ngModel',
    link: function(scope, element, attr, ngModel) {
      ngModel.$formatters.push(function(value) {
        // Do stuff here, and return the formatted value.
      });
  };
};
Run Code Online (Sandbox Code Playgroud)