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

标签 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>

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'
    }
}

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>

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

我想点击按钮设置输入元素值,angularjs(ng-model 或 scope 对象)可以获取它。

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

通过这种方式改变元素值,angularjs $watch 和 bind 函数都获取不到值改变事件。如果您通过键盘在 input 元素中输入一些单词,它们都有效。

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

最佳答案

首先,双向数据绑定(bind)是为了避免这种情况。

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

 <button ng-click='ctrl.field = "new value"'>change</button>

其次,ng-change 指令与 ng-model 协同工作,可用于在模型中的值发生变化时执行某些操作。

 <input ng-model='ctrl.field' ng-change='alert("changed!")' />

如果您仍想直接使用 DOM 更改 Angular 之外的值,只需触发 Angular 监听的事件 - blurkeypressed

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

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

关于javascript - 在angularjs中设置值时如何检测输入元素的变化,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23811158/

相关文章:

javascript - 我可以在angular js asp.net mvc中调用Controller中的Route Provider服务吗

javascript - templateUrl 函数未使用显式注解,无法在严格模式下调用

javascript - 使用 npm 安装本地模块?

javascript - 在 alexa sdk v2 responsebuilder 中暂停

javascript - 输入文件 - 更改数据按钮文本的文本按钮

angularjs - 如何将复杂属性从父范围继承到指令的隔离范围中

javascript - 避免重复的 json 数组对

php - 我想在用户右键单击时更改另存为对话框文件名

AngularJS - 范围未更新

javascript - Angular Material 选择显示在对话框下