繁体   English   中英

在Angular指令中动态更新模板的HTML

[英]Dynamically updating the template's Html in an Angular directive

我有一个名为customStyles的自定义指令,其定义为:

scrollbackApp.directive('customStyles', function(){
    return{
        restrict: 'E',
        template: '<style> {{styleString}} </style>',
        scope: {
            conversations : '='
        },
        link: function($scope, elem, attrs){
            $scope.$watch('conversations', function(value){
                // calculate str based on value
                $scope.styleString = str;
            });
        }
    }
});

我将此指令添加到我的HTML视图的正文中:

<body>
  <custom-styles conversations="convList"> </custom-styles>
</body>

并且convList的值在父控制器范围内更改。 目前,当上述指令呈现为Html时, {{styleString}}绑定将保留为字符串,而不是更改为其值。 我希望指令的html根据styleString的值进行动态更改。 在Angular中可能吗?

您应该在视图中重命名指令标记:

<custom-styles conversations="convList"> </custom-styles>

这是工作示例:

http://plnkr.co/edit/aOxf3fgS7Tc0sOyCXoBV?p=preview

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM