Angularjs - 使用指令实例化其他指令
Angularjs - Using directive to instantiate other directives?
所以让我们在我的HTML中说我有这样的东西:
<tabcontent></tabcontent>
那么这个指令的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;
});
HTML 被正确解析,类型值在控制器 JS 中找到。
so <tabcontent></tabcontent> is replaced with <div recipe></div> for example..
(该部分确实正确发生)
所以我对食谱也有一条指令:
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;
});
这显然非常简单,仅用于测试。但是配方指令没有被处理...
这是怎么回事?
您需要更改 2 件事:
-
recipe
指令不得限制为 E(元素)。如果要生成像<div recipe></div>
这样的指令,则必须至少将 A(属性)添加到指令配置的restrict
属性中:app.directive('recipe', function() { return { restrict: 'E', ...
-
你需要在"watch"之后编译
tabcontent
指令的 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); }); } ...
jsFiddle: http://jsfiddle.net/bmleite/n2BXp/
相关文章:
- 如何在GoogleWeb工具包(GWT)中从JSNI调用接口(实例化)
- 如何在用户输入时实例化数组
- 在requirejs中共享实例化对象
- Mongoose TypeError:实例化模式类型的对象时,对象不是函数
- Angular重命名模块后未能实例化模块
- Backbone relational无法实例化两个RelationalModel对象
- 我应该如何用javascript实例化这个日期
- AngularJS出错-无法实例化模块
- 谷歌地图重叠MarkerSpiderfier实例化问题
- 未捕获错误:由于,[$injector:moduler]未能实例化模块polmgr
- ngRoute不是因果报应实例化的
- JavaScript 中的动态实例化
- 在文本框上绑定谷歌地点自动完成,而无需实例化谷歌地图
- UI5 路由不实例化视图
- 在 JavaScript 中,当我们实例化派生对象时,原型的函数隐藏在哪里
- 从指令 AngujarJs 实例化映射
- 实例化一个对象,使其可以与多个指令共享
- 角指令单元测试中的模拟控制器实例化
- Angularjs - 使用指令实例化其他指令
- 未捕获错误:[$injector:modulerr]实例化模块失败;当有多个“ng-app"”指令