大佬教程收集整理的这篇文章主要介绍了angularjs – 将父指令属性传递给子指令属性,大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
<parent-dir menu-template="this.html" item-template="that.html"></parent-dir>@H_197_8@
<ul style="list: none" ng-repeat="item in menu"> <child-dir template="{{itemTemplatE}}"></child-dir> </ul>@H_197_8@我的指令看起来像这样:
angular.module('myApp') .directive('parentDir',function () { return { reStrict: 'E',scope: { menuTemplate: '@',itemTemplate: '@',menuType: '@',menuName: '@',menuId: '@',},templateUrl: function (element,attrs) { alert('Scope: ' + attrs.menuTemplatE); return attrs.menuTemplate; },controller: function ($scope,$element,$attrs) { $scope.defaultSubmit = false; alert('Menu: '+$attrs.menuTemplatE); alert('Item: ' + $attrs.itemTemplatE); $scope.itemTemplate = $attrs.itemTemplate; if ($attrs.$attr.hasOwnProperty('defaultSubmit')) { alert('It does'); $scope.defaultSubmit = true; } } }; }) .directive('childDir',function () { return { reStrict: 'E',require: '^parentDir',attrs) { alert('Item Template: ' + attrs.templatE); return attrs.template; },$attrs) { $scope.job; alert('Under job: ' + $scope.itemTemplatE); } }; });@H_197_8@
换句话说:在templateUrl函数运行时,template属性的值仍为“{{itemTemplatE}}”.在指令的链接(preLink精确)功能运行之前,情况仍然如此.
我创建了一个plunker来演示here点.确保打开控制台.您将看到templateUrl在父链接函数和子链接函数之前运行.
那你做什么呢?
幸运的是,angular提供了一个$templaterequest服务,它允许您以与使用templateUrl相同的方式请求模板(它还使用了方便的$templateCachE).
$templaterequest(attrs.templatE) .then(function (tplString){ // compile the template then link the result with the scope. contents = $compile(tplString)(scopE); // Insert the compiled,linked element into the DOM elem.append(contents); })@H_197_8@
以上是大佬教程为你收集整理的angularjs – 将父指令属性传递给子指令属性全部内容,希望文章能够帮你解决angularjs – 将父指令属性传递给子指令属性所遇到的程序开发问题。
如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。
本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。