Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/395.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript AngularJS将值从指令链接函数传递给控制器_Javascript_Angularjs - Fatal编程技术网

Javascript AngularJS将值从指令链接函数传递给控制器

Javascript AngularJS将值从指令链接函数传递给控制器,javascript,angularjs,Javascript,Angularjs,新手到有棱角的。非常简单的问题。我有以下代码 我只想显示下面的文件计数。我将fileCount变量绑定到作用域,但它不起作用 var-app=angular.module('fileUploader',[]); 应用程序控制器('上传'),功能($范围){ $scope.fileCount=0; }) .directive(“customDirective”,function()){ 返回{ 链接:功能(范围、el、属性){ el.bind(“变更”,功能(事件){ log(event.tar

新手到有棱角的。非常简单的问题。我有以下代码

我只想显示下面的文件计数。我将fileCount变量绑定到作用域,但它不起作用

var-app=angular.module('fileUploader',[]);
应用程序控制器('上传'),功能($范围){
$scope.fileCount=0;
})
.directive(“customDirective”,function()){
返回{
链接:功能(范围、el、属性){
el.bind(“变更”,功能(事件){
log(event.target.files.length);
scope.fileCount=event.target.files.length;
});
}
}
});

文件计数为:{{fileCount}

该指令确实从其父级继承了作用域属性,但它不知道在您更改对父级属性的引用时启动摘要循环,因此您必须手动执行此操作(请查看此项):


这将启动摘要周期,您将看到更新按预期进行。

谢谢您的回答!
.directive("customDirective", function(){
    return{
        link: function(scope, el, attrs){
            el.bind("change", function(event){
              scope.fileCount = event.target.files.length;

              scope.$apply(); // notice this
            });
        }
    }
});