Angularjs – 无法捕捉$watch事件

前端之家收集整理的这篇文章主要介绍了Angularjs – 无法捕捉$watch事件前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我试图在我的代码中实现这个指令而没有多少succces. angular-material-fileinput.

我按照github页面中的说明添加“lfNgMdFileInput”到我的模块依赖项中,在HTML中添加了html标记,除了一件事之外,一切都很好.

根据文档,我通过使用$scope.’watch(‘lfFilesName’,function(old,new){…})来捕获字段中输入的数据的方式.

指令处理的数据很好,并且在指令$scope中保存所有内容都没有问题,但$watch事件永远不会被触发,除非它被初始化.

如果有任何我应该添加内容,请告诉我.代码本身就是这样的:

// the piece of html
<lf-ng-md-file-input lf-files="files02" lf-api="api02" multiple></lf-ng-md-file-input>

// the piece of js; PS: While the selected files appear on the directive scope,they don't appear in the scope of my controller,//  witch i guess is normal but it would still have to catch the watch event,right?
$scope.$watch('files02.length',function(newVal,oldVal){
  console.log($scope.files02);
});

Edit1 :(代码片段)

<body  ng-controller="HomeCtrl">
<div ui-view layout="row" ng-style="checkHeightReq()">
    <md-tabs md-selected="selectedIndex"  md-dynamic-height md-stretch-tabs="always" flex="100" md-border-bottom="">
        <md-tab label="{{lang.uploadDocuments}}" ng-disabled="tabsDisabled.tab4">
              <div ng-include="'partials/tabUploadDocuments.html'" ng-controller="UploadDocCtrl"></div>
        </md-tab>
    </md-tabs>
</div>
</body>

Partials / tabUploadDocuments.html:

<lf-ng-md-file-input lf-files="files01"></lf-ng-md-file-input>
  <lf-ng-md-file-input lf-files="files02"></lf-ng-md-file-input>

UploadDocCtrl:

angular.module('demo').controller('UploadDocCtrl',UploadDocuments);


UploadDocuments.$inject = ['$scope','LangVars','$mdMedia','$mdToast'];

function UploadDocuments($scope,LangVars,$mdMedia,$mdToast) {
//unrelated stuff
 $scope.$watch('files02',oldVal){
                     console.log($scope.files02);
                 },true);   
 $scope.$watch('files01',oldVal){
                      console.log($scope.files02);
                  });

//unrelated stuff
}

解决方法

Watch将在Model属性上运行.您可以将代码更改为仅使用“files02”(即无长度).

$scope.$watch('files02',oldVal){
                 console.log($scope.files02);
     });

猜你在找的Angularjs相关文章