我正在开发一个小部件,我想一个接一个地呈现一些消息/文本.我想根据消息类型更改消息的模板.
我当前的指令设置如下
directive('cusMsgText', function(){ return { restrict: 'E', template:function(elements, attrs){ return ''; }, link: function($scope, iElm, iAttrs, controller) { //add children to iElm based on msg values in $scope } }; });
该指令使用如下
现在我的问题是 - :
是否可以根据属性的实际值从模板函数本身返回多个字符串(模板)中的一个
msg
.我尝试访问attrs.msg
模板功能,然后返回value.type
.
如果没有那么,操作下面的模板是好linker
还是我需要将它移动到compile
功能?
link.. 7
要基于value.type
您呈现不同的模板,可以使用以下ng-switch
语句:
//...template for type 1 here...//...template for type 2 here...
另外,如果我理解你的第二个问题:应该在compile
函数中完成对未编译指令的操作,编译后发生的所有操作都应该放在link
函数中.
ngSwitch的文档
编辑:给塞巴斯蒂安+1以了解你想要的东西.然而,他提出的建议主要是重新发明轮子,因为它基本上是手动编译和插入模板(这ngSwitch
对你有用).此外,您可以通过函数的attrs
参数访问您在指令上放置的属性link
.