onchange不会改变AngularJS视图中的$ scope

时间:2016-07-13 00:07:41

标签: javascript angularjs view angularjs-scope

我有以下代码(见下文)。当我单击按钮并选择文件时,视图中的data.myVar保持不变。 (不知何故,我确实看到$scope.data.myVar在步进控制器时正确更新了。为什么会这样?

html摘录(它位于以下控制器下):

{{data.myVar}}

<input type="file" onchange="angular.element(this).scope().setRuleFile(this)" />

JS:

angular.module('myModule')
    .controller('MyCtrl', ['$scope', function ($scope) {

    $scope.data = {
        myVar: 'init'
    };

    $scope.setRuleFile = function(element) {
        $scope.data.myVar = 'changed';
    }

}]);

1 个答案:

答案 0 :(得分:2)

使用指令而不是hacky。这是一个类似的问题: AngularJs: How to check for changes in file input fields?