Javascript 创建角度指令表示未定义不是函数
我想创建自己的treeview指令,但出现以下错误:Javascript 创建角度指令表示未定义不是函数,javascript,angularjs,Javascript,Angularjs,我想创建自己的treeview指令,但出现以下错误: TypeError: undefined is not a function 代码是 我的指令代码是: app.directive('tree', [function () { return { scope:{ treeModel:'=' }, restrict: 'AE', template:'<ul><li ng-repeat="root in treeModel"&
TypeError: undefined is not a function
代码是
我的指令代码是:
app.directive('tree', [function () {
return {
scope:{
treeModel:'='
},
restrict: 'AE',
template:'<ul><li ng-repeat="root in treeModel">{{root.name}}'+
'<ul><li ng-repeat="h in root.hierarchies"><hierarchey hierarchey- model="h"></hierarchey></li></ul>'
+'</li><ul>'
};
}]);
app.directive('hierarchey', [function () {
return {
scope:{
isExpand:false
},
controller:function($scope){
$scope.hierarchyOp = function(){
alert("IM CLIKED");
}
},
restrict: 'AE',
template:'<span ng-click="hierarchyOp()"><i ng-show="isExpand" class="fa fa-folder-open"></i><i ng-hide="isExpand" class="fa fa-folder-open"></i>{{h.name}}</span>'
};
}])
app.directive('tree',[function(){
返回{
范围:{
树模型:'='
},
限制:“AE”,
模板:“- {{root.name}”+
“
”
+“
”
};
}]);
应用指令('hierarchey',[function(){
返回{
范围:{
isExpand:错误
},
控制器:功能($scope){
$scope.hierarchyOp=函数(){
警报(“即时消息”);
}
},
限制:“AE”,
模板:“{h.name}”
};
}])
问题的第一部分是两个指令都有隔离作用域
因此,为了使hierarchey指令能够通过heirArcheModel变量访问h变量,您需要将值传递给该指令
scope:{
hierarcheyModel: '=' //add this to pass the object to the scope
}
第二部分是由于ng repeat也创建了自己的范围,据我所知,它不是父对象的子范围。因此,您需要隔离作用域,并且必须将变量传递到指令中才能访问它
树:
app.directive('tree', [function () {
return {
scope:{
treeModel:'='
},
restrict: 'AE',
template:
'<ul>'+
'<li ng-repeat="root in treeModel">{{root.name}}'+
'<ul>' +
'<li ng-repeat="h in root.hierarchies">' +
'<hierarchey hierarchey-model="h"></hierarchey>' +
'</li>' +
'</ul>' +
'</li>'+
'</ul>' //Forgot to close the ul
};
}]);
app.directive('tree',[function(){
返回{
范围:{
树模型:'='
},
限制:“AE”,
模板:
“”+
“- {{root.name}”+
“
”+
“- ”+
'' +
“
”+
“
”+
“ ”+
“
”//忘记关闭保险柜
};
}]);
等级制度
app.directive('hierarchey', [function () {
return {
scope:{
hierarcheyModel: '=' //add this to pass the object to the scope
},
controller:function($scope){
$scope.hierarchyOp = function(){
alert("IM CLIKED");
}
$scope.isExpand = false; // This value should like in the controller not the isolate scope
},
restrict: 'AE',
template:'<span ng-click="hierarchyOp()"><i ng-show="isExpand" class="fa fa-folder-open"></i><i ng-hide="isExpand" class="fa fa-folder-open"></i>{{hieracheyModel.name}}</span>'
};
}])
app.directive('hierarchey',[function(){
返回{
范围:{
hierarcheyModel:“=”//添加此项以将对象传递到作用域
},
控制器:功能($scope){
$scope.hierarchyOp=函数(){
警报(“即时消息”);
}
$scope.isExpand=false;//此值应该在控制器中,而不是在隔离作用域中
},
限制:“AE”,
模板:“{hieracheyModel.name}”
};
}])
我没有深入研究代码,只是试图解决主要问题。
问题源于您没有声明应用程序本身
请看这里:
我已经声明了这个应用程序,问题已经解决了(关于您代码的其余部分-这是一个不同的问题:)