Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angularjs/25.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指令代码吗? 我来自更严格的语言,如java、C++、C++,并且一直使用这个代码来读取文件到文本文本,而不理解它是如何工作的。现在我试图学习指令,以便理解该指令,但我很难理解该代码的一部分_Javascript_Angularjs_Angularjs Directive_Parameter Passing - Fatal编程技术网

Javascript 有人能解释一下这段angularjs指令代码吗? 我来自更严格的语言,如java、C++、C++,并且一直使用这个代码来读取文件到文本文本,而不理解它是如何工作的。现在我试图学习指令,以便理解该指令,但我很难理解该代码的一部分

Javascript 有人能解释一下这段angularjs指令代码吗? 我来自更严格的语言,如java、C++、C++,并且一直使用这个代码来读取文件到文本文本,而不理解它是如何工作的。现在我试图学习指令,以便理解该指令,但我很难理解该代码的一部分,javascript,angularjs,angularjs-directive,parameter-passing,Javascript,Angularjs,Angularjs Directive,Parameter Passing,这是我的控制器的函数,我希望被调用。fileContent只是一个文本字符串: self.displayFileContent = function(contents) { self.fileContent = contents; }; 这是read file指令的实现。我希望调用是displayFileContentFn(fileContents),但为什么它要用完全不同的参数(和不同数量的参数)调用函数呢?{'contents:filecontents}是javascript对象吗

这是我的控制器的函数,我希望被调用。fileContent只是一个文本字符串:

self.displayFileContent = function(contents) {
    self.fileContent = contents;
};
这是read file指令的实现。我希望调用是
displayFileContentFn(fileContents)
,但为什么它要用完全不同的参数(和不同数量的参数)调用函数呢?{'contents:filecontents}是javascript对象吗

scope: false,
link: function(scope, element, attrs) {
        element.bind('change', function(e) {

            var displayFileContentFn = $parse(attrs.onFileChange);
            var reader = new FileReader();

            reader.onload = function() {
                var fileContents = reader.result;

                scope.$apply(function() {
                    displayFileContentFn(scope, {
                        'contents' : fileContents
                    });
                });
            };
            reader.readAsText(element[0].files[0]);
        });
    }

这与angular的$parse服务有关

如果绑定“在文件上更改”属性,如:

on-file-change='displayFileContent(content)'
displayFileContenFn由以下内容定义:

var displayFileContentFn = $parse(attrs.onFileChange);
事实上,给出的是这样的东西(真正的代码在这里要复杂得多,我只是简化了它以便于理解):

所以这里

displayFileContentFn(scope, {
                        'contents' : fileContents
                    });
这意味着使用
范围
执行
显示文件内容
(因为我们将其绑定到on file change属性),将文件内容作为
内容
参数传递(我们在属性中将其声明为第一个参数)


这样,当读取器完成读取时,读取的文件内容将作为
内容
传递给
displayFileContent
函数。

要了解这里发生的事情,您必须了解angular的生命周期是如何工作的

本质上,您是在向
$scope.$apply
提供表达式,而不是函数。该表达式是使用
$scope.$eval
执行的,允许根据提供的
scope
对表达式求值。(在这种情况下,
范围由指令定义)表达式求值后,处理任何异常处理,然后可以触发任何监视

伪代码$apply()

在函数的情况下,如果您的HTML是
on file change='displayFileContent(contents)
$scope.$apply()

displayFileContentFn(scope, {
    'contents' : fileContents
});

并从HTML返回
displayFileContent(fileContents)
,其中
displayFileContentFn==onFileChange
,从JavaScript函数返回
contents==fileContents
。然后执行此命令。

请提供指令的作用域部分。即作用域:{}谢谢,此简化版本有助于清理它。
{'contents':fileContents}
中的'contents'必须与
displayFileContent(contents)
中的参数名称匹配,对吗?或者它与其他地方的名字相匹配?我尝试了
“dummy”
,但失败了。是的,它们必须匹配。您可能认为angular提供的$parse服务是一种将字符串表达式解析为真实javascript代码的解析器。谢谢,我感谢您的帮助。我的问题是为什么调用
displayFileContentFn()
时使用了2个参数而不是1个参数。不在$apply上,怎么可能
displayFileContentFn(arg1,arg2)
被传递到
apply
,此时它已经有了2个参数。它所做的只是调用一个已有2个参数的函数,不是吗?
function $apply(expr) {
  try {
    return $eval(expr);
  } catch (e) {
    $exceptionHandler(e);
  } finally {
    $root.$digest();
  }
}
displayFileContentFn(scope, {
    'contents' : fileContents
});