angularjs - 将变量传递给指令模板

标签 angularjs angularjs-directive angularjs-scope

我想做的是这样的:

<rule-type-selector class="protractor-test-rule-description" all-rule-types="allRuleTypes" local-value="$parent.$parent.currentRuleDescription" on-selection-change="onSelectNewRuleType()" disabled="disabled">
</rule-type-selector>

在指令中

  return {
    restrict: 'E',
    scope: {
      allRuleTypes: '&',
      localValue: '=',
      onSelectionChange: '&',
      disabled: '=' 
    },
    template: function($scope) {
      if($scope.disabled) {
        return '<input type="hidden" disabled>';
      }
      return '<input type="hidden">';
    }

这行不通。无论是否设置了禁用,我总是点击第二个返回语句。 我看过 Passing value of a variable to angularjs directive template function但我的用例似乎有点不同。

最佳答案

您需要在指令的 link 函数中使用 $compile 服务。

您需要一个创建模板的函数(比方说:getTemplate),然后在您使用的链接函数中:

    element.html(getTemplate(parameter1, parameter2, ...));

然后:

        $compile(element.contents())(scope);

这是应用示例: http://jsfiddle.net/hwndew2o/4/

关于angularjs - 将变量传递给指令模板,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26885529/

相关文章:

javascript - 使用 Angular 将数组从模板绑定(bind)到 Controller

AngularJS - 基于 null 和空数组的隐藏值

angularjs - AngularJS 中的范围未更新

javascript - angularjs 可拖动指令在内部显示值

angularjs - 在angular js项目中强制更新html5视频源

javascript - 全局函数中的 Angular 问题,自 beta 1.3.0b15 以来已损坏。需要帮助请

angularjs - 我们可以在 ui-router angular.js 中使用已经存在的状态向 $stateprovider 添加动态状态吗

javascript - 有没有办法在 AngularJS 中同时订阅多个事件?

javascript - 从 Controller 注入(inject) Angular 元素但不按指令工作

angularjs - 在没有隔离范围的情况下将变量传递给 AngularJS 指令