Javascript 在angularjs中将数组添加到作用域时出现TypeError
我不熟悉angular和javascript。我正在尝试构建一个指令,其中一部分我需要向该指令的作用域添加一个数组。我的指令代码如下所示: .直接启动部分、功能{ 返回{ 限制:E,, 是的, 范围:{ 标题:@, 章节:[1,2,3] }, 链接:functionscope、elem、attrs{ console.log范围; } } }; 执行时,我得到Javascript 在angularjs中将数组添加到作用域时出现TypeError,javascript,angularjs,Javascript,Angularjs,我不熟悉angular和javascript。我正在尝试构建一个指令,其中一部分我需要向该指令的作用域添加一个数组。我的指令代码如下所示: .直接启动部分、功能{ 返回{ 限制:E,, 是的, 范围:{ 标题:@, 章节:[1,2,3] }, 链接:functionscope、elem、attrs{ console.log范围; } } }; 执行时,我得到 TypeError: definition.match is not a function at angular.js:7992
TypeError: definition.match is not a function
at angular.js:7992
at forEach (angular.js:417)
at parseIsolateBindings (angular.js:7987)
at parseDirectiveBindings (angular.js:8028)
at addDirective (angular.js:9984)
at collectDirectives (angular.js:9142)
at compileNodes (angular.js:8974)
at compileNodes (angular.js:8990)
at compileNodes (angular.js:8990)
at compile (angular.js:8859)
如果我用字符串或数字替换节的值,错误就会消失。是否可以将数组作为作用域中属性的值?如果是,如何执行?指令中的范围对象不是实例化范围变量的地方。scope对象由键值对组成,这些键值对应该将指令的作用域变量映射到指令所在的控制器的变量 因此,如果“section”数组包含必须从控制器提供给指令的值,则需要在控制器中定义数组,并通过将数组名称作为属性传递并使用以下构造将其绑定到指令:
scope: {
title: "@",
sections: "="
},
控制器应包含以下内容:
$scope.sectionsArray = [1,2,3]
将绑定到视图中的指令。”“节”现在可以作为指令中的作用域属性访问
<startup-sections title='SomeTitle' sections="sectionsArray" >
希望能有帮助
.directive("startupSections", function(){
return {
restrict: "E",
transclude: true,
scope: {
title: "@"
},
link: function(scope, elem, attrs){
scope.sections = [1,2,3]
console.log (scope);
}
}
});