如何在angularjs中设置值时检测输入元素的变化

Gin*_*ino 9 javascript angularjs

在线代码:

http://jsfiddle.net/mb98y/309/

HTML

<div ng-app="myDirective" ng-controller="x">
    <input id="angular" type="text" ng-model="data.test" my-directive>
</div>
    <button onclick="document.querySelector('#angular').value = 'testg';">click</button>
Run Code Online (Sandbox Code Playgroud)

JS

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);
                alert('value change: ' + scope.data.test);
            });
        }
    };
});

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

http://jsfiddle.net/mb98y/310/

HTML

<div ng-app="myDirective" ng-controller="x">
<input id="angular" type="text" my-directive="test">{{test}}</div>
<button onclick="document.querySelector('#angular').value =  'testg';">click</button>
Run Code Online (Sandbox Code Playgroud)

JS

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 change 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)

我想点击按钮设置输入元素值和angularjs(ng-model或范围对象)可以得到它.

但, document.querySelector('#angular').value = 'testg';

以这种方式改变元素值,angularjs $ watch和bind函数都不能获得值改变事件.如果您通过键盘在输入元素中输入一些单词,它们都可以正常工作.

在angularjs中以这种方式设置值时,如何检测输入元素值的变化?

vit*_*ore 14

首先,这里是双向数据绑定,以避免这种情况.

 <input ng-model='ctrl.field' />

 <button ng-click='ctrl.field = "new value"'>change</button>
Run Code Online (Sandbox Code Playgroud)

其次,有一个ng-change与之配合使用的指令,ng-model当模型中的值发生变化时,它可用于执行某些操作.

 <input ng-model='ctrl.field' ng-change='alert("changed!")' />
Run Code Online (Sandbox Code Playgroud)

如果您仍希望直接使用DOM更改角度以外的值,只需触发有角度侦听的事件 - blurkeypressed

 <button ng-click='$("#id").val(a); $("#id").trigger("blur")'>change</button>
Run Code Online (Sandbox Code Playgroud)

要么

 <button ng-click='angular.element("#id").val(a); angular.element("#id").trigger("blur")'>change</button>
Run Code Online (Sandbox Code Playgroud)