Angular1.3:递归指令模板中的访问控制器函数
Angular1.3: Access controller function inside recursive directive template?
我可以使用$parent操作符在父指令中访问一次控制器函数,但这在递归子指令中不起作用。下面是我的代码示例(我试着把它缩短一点):
//控制器示例(使用controllerAs定义):——
var View2Ctrl = function(){
//function i am trying to access:
this.getNumber = function(){
return 5;
}
.
.
.
angular.controller('View2Ctrl',............
//'comments'指令模板:——
<li>
<!-- I'm trying to access the function in here: -->
<!-- below line works just once here, does not load in recursive child directives below: -->
<strong> Number: </strong> {{ $parent.View2Ctrl.getNumber() }}
<!-- below line gets replaced with a recursive child directive again -->
<span class="comments-placeholder" ></span>
</li>
//'comments' directive.js:——
var comments = function($compile){
return {
templateUrl : 'modules/comments/commentsDirective.html',
restrict:'E',
scope:{
collection: '='
},
link: function(scope, element, attrs){
if(scope.collection.data.replies){
var elementResult = angular.element(element[0].getElementsByClassName('comments-placeholder'));
elementResult.replaceWith($compile('<ul ng-repeat="comment in collection.data.replies.data.children><comments collection="comment"></comments></ul>')(scope));
}
}
};
};
可以通过require
访问父控制器的this
var comments = function($compile){
return {
...
require: '^ngController',
link: function(scope, element, attrs, ctrl){
$scope.number = ctrl.getNumber();
}
};
};
但是最好有一个服务作为模型,为控制器和指令保存数据。
相关文章:
- 否'访问控制允许来源'标头存在于IISNOde中请求的资源(AngularJS+NodeJs)上
- CORS:否'访问控制允许来源'header-但是php设置头文件
- "访问控制允许起源”;通过javascript从http页面调用同一网站的httpsurl时出现问题
- CORS:访问控制允许原点不等于提供的原点
- 飞行前响应中的访问控制允许标头不允许Angularjs请求标头字段访问控制允许标题
- django没有访问控制允许删除原始标头
- HTML5 Canvas访问控制允许原始错误
- Android、iOS和Windowsphone的PHP访问控制
- 访问控制台中的匿名函数
- 访问控制允许标头不允许X-Requested-Wise
- 我如何设置HTTP头“;访问控制允许起源”;用于来自thttpd web服务器的HTTP响应
- 如何通过脚本访问用户控制值
- Ajax—“;访问控制允许起源”;错误
- XMLHttpRequest无法加载url Origin访问控制允许Origin不允许null
- 如何访问访问过的链接历史记录
- 访问允许控制源失败,并带有 www 前缀
- 要在Parse.com中使用plupload,如何访问文件控制对象或base64字符串
- 使用postmessage进行跨域iframe访问-访问被拒绝
- 访问浏览器控制jQuery
- 是否可以访问和控制浏览器的页面大小调整功能?