【问题标题】:Passing a parent directive attribute to a child directive attribute将父指令属性传递给子指令属性
【发布时间】:2015-10-27 04:18:00
【问题描述】:

我正在为客户可以使用的库创建指令。我需要让客户为指令创建自己的模板,并将该模板的绝对 url 值传递给我的指令。我的一个指令将在其中包含另一个自定义指令,并且它的模板将根据父指令的属性之一的值来计算。这是一个例子:

<parent-dir menu-template="this.html" item-template="that.html"></parent-dir>

我有一个用于该指令的模板,如下所示:

<ul style="list: none" ng-repeat="item in menu">
    <child-dir template="{{itemTemplate}}"></child-dir>
</ul>

我的指令如下所示:

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',
        templateUrl: function (element, attrs) {
            alert('Item Template: ' + attrs.template);
            return attrs.template;
        },
        controller: function ($scope, $element, $attrs) {                
            $scope.job;
            alert('Under job: ' + $scope.itemTemplate);
        }
    };
});

我没有显示所有代码,但这是我的问题的主要部分。当我运行它时,我一直在 childDir 上的模板未定义。

从 parentDir 永久保存 itemTemplate 的值以便 childDir 可以将其用作模板的最佳做法是什么?

【问题讨论】:

    标签: angularjs angularjs-directive angularjs-scope


    【解决方案1】:

    您遇到问题的原因是因为生成 templateUrl 的函数在将 scope 分配给您的指令之前运行 - 在替换插值数据之前必须完成此操作。

    换句话说:在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);
        })
    

    然后,您可以删除指令定义对象中对 template 的任何引用,一旦插入属性,这将安全运行。

    【讨论】:

    • 感谢您的帮助!我用这种方式尝试过,它有效!另外,我尝试使用模板,并传递了一个函数,该函数将通过将模板设置为字符串并以这种方式将 attrs.itemTemplate 添加到子指令上来添加子指令的属性。
    • 用例略有不同,但基本问题相同。这个答案很有效。 @Michael,如果您在附近,您是否认为值得接受这个答案来帮助像我这样的其他人,表明这是一个很好的答案?
    猜你喜欢
    • 1970-01-01
    • 2013-07-08
    • 2013-05-08
    • 1970-01-01
    • 1970-01-01
    • 2016-05-23
    • 2013-04-23
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多