Ell*_*iot 11 javascript angularjs
所以我可以在我的HTML中说我有这样的事情:
<tabcontent></tabcontent>
Run Code Online (Sandbox Code Playgroud)
然后这个指令的javascript是这样的:
tabsApp.directive('tabcontent', function(){
var myObj = {
priority:0,
template:'<div></div>',
replace: true,
controller: 'TabCtrl',
transclude: false,
restrict: 'E',
scope: false,
compile: function (element, attrs){
return function (parentScope, instanceEle){
parentScope.$watch('type', function(val) {
element.html('<div '+val+'></div>');
});
}
$compile(parentScope);
},
link: function postLink(scope, iElement, iAttrs){}
};
return myObj;
});
Run Code Online (Sandbox Code Playgroud)
正确解析HTML,并在控制器JS中找到type的值.
so <tabcontent></tabcontent> is replaced with <div recipe></div> for example..
Run Code Online (Sandbox Code Playgroud)
(那部分确实发生了)
所以我也有食谱指令:
tabsApp.directive('recipe', function(){
var myObj = {
priority:0,
template:'<div>TESTING</div>',
replace: true,
controller: 'TabCtrl',
transclude: false,
restrict: 'E',
scope: false,
compile: function (element, attrs){
return {
pre: function preLink(scope, iElement, iAttrs, controller){},
post: function postLink(scope, iElement, iAttrs, controller){}
}
},
link: function postLink(scope, iElement, iAttrs){}
};
return myObj;
});
Run Code Online (Sandbox Code Playgroud)
这显然非常简单,仅用于测试.但是配方指令没有被处理......
这里发生了什么?
bml*_*ite 19
你需要改变两件事:
该recipe指令不得限于E(元素).如果要生成指令<div recipe></div>,则必须至少在指令配置上向属性添加A(属性)restrict:
app.directive('recipe', function() {
return {
restrict: 'E',
...
Run Code Online (Sandbox Code Playgroud)你需要tabcontent在'watch'之后编译指令的HTML内容:
app.directive('tabcontent', function($compile){
return {
...
link: function (scope, iElement, iAttrs) {
scope.$watch('type', function(val) {
iElement.html('<div '+val+'></div>');
$compile(iElement.contents())(scope);
});
}
...
Run Code Online (Sandbox Code Playgroud)jsFiddle:http://jsfiddle.net/bmleite/n2BXp/
| 归档时间: |
|
| 查看次数: |
8318 次 |
| 最近记录: |