【问题标题】:passing value from parent directive to child directive's template function将值从父指令传递到子指令的模板函数
【发布时间】:2018-03-23 23:00:04
【问题描述】:

我正在尝试访问从父指令传递到子指令模板函数的值。

请参考下面的插件。

Plunker Link

代码:

父指令:

directive('parentDir', function(){
  return {
    controller: ['$scope',function($scope){
      $scope.myVal = 'HELLO';
    }],
    templateUrl: 'parentDir.html'
  }
})

儿童指令:

directive('childDir', function(){
  return {
    template: function(element,attrs){
      alert(attrs.val);
    }
  }
})

parentDir.html:

<div>
  <child-dir val="{{myVal}}"></child-dir>
</div>

【问题讨论】:

    标签: angularjs angularjs-directive angularjs-scope angularjs-model


    【解决方案1】:

    您可以像这样将val 属性添加到指令中:

    .directive('childDir', function(){
      return {
        restrict: 'E',
        scope : {
          val : '='
        },
        link : function(scope, element, attrs) {
          return alert(scope.val);
        }
      }
    })
    

    这是一个有效的plunkr

    【讨论】:

    • 感谢您的回答。但我想要模板函数上的值而不是链接函数上的值。
    • 嗯,我对模板功能不是很熟悉。您是否已经遇到过这个question
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-03-10
    • 2014-10-12
    • 2015-10-27
    • 1970-01-01
    • 2015-06-10
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多