将属性指令传递给元素指令
Pass an attribute directive to an element directive
我尝试将属性指令传递给元素指令,这是可能的吗?我试着像例子中那样做,但它不起作用。
例如,我有元素指令: <np-form-input
np-form-input-attrs="np-my-attr-directive"
>
</np-form-input>
JS:
.directive('npFormInput', [function () {
return{
restrict: 'E',
templateUrl: '/resources/view/common/form_input',
link: function(scope, element, attr){
scope.attributes= attr.npFormInputAttrs;
}
};
}])
然后在指令HTML
<input
{{attributes}}
>
提前感谢。
编辑:我的解决方案基于Micah Williamson的回答:
.run(['$templateCache', '$http', function($templateCache, $http){
$http.get('/resources/view/common/form_input').success(function(data){
$templateCache.put('/resources/view/common/form_input', data);
});
}])
.directive('npFormInput', ['$templateCache', '$compile', function ($templateCache, $compile) {
return{
restrict: 'E',
compile: function (ele, attrs) {
var tpl = $templateCache.get('/resources/view/common/form_input');
tpl = tpl.replace('{{attributes}}', attrs.npFormInputAttrs);
var tplEle = angular.element(tpl);
ele.replaceWith(tplEle);
return function (scope, element, attr) {
$compile(tplEle)(scope);
};
},
};
}])
我已经做了一些类似于你想做的事情,但我不得不在编译中注入属性。不过,您需要先将模板添加到$templateCache
。
.directive('npFormInput', [function ($templateCache, $compile) {
return{
restrict: 'E',
compile: function(ele, attrs) {
var tpl = $templateCache.$get('/resources/view/common/form_input');
tpl = tpl.replace('{{attributes}}', attrs.npFormInputAttrs);
var tplEle = angular.element(tpl);
ele.replaceWith(tplEle);
return function(scope, element, attr){
$compile(tplEle)($scope);
};
}
};
}])
相关文章:
- 指令的模板必须只有一个根元素:With restrict E&替换true
- AngularJS指令,在元素后插入HTML
- AngularJS-将点击事件绑定到指令元素的子元素
- 离子指令元素不调用函数(绑定)更改
- 指令元素未显示
- 访问链接函数内的当前自定义指令元素
- 将指令属性添加到指令元素
- 将更新绑定到链接中的指令元素,而不是内联
. - AngularJs 指令元素子元素给出空
- AngularJS,指令元素为空
- 通过父控制器 AngularJS 设置指令元素属性
- AngularJS:如何判断我的控制器是否绑定到我的ng-repeat指令元素?我的数据没有显示
- JqueryDatePicker角度指令元素不工作
- 如何在现有的父指令元素和子指令元素之间动态嵌套指令元素
- Angular指令元素
- Angular指令-如何使用JQuery将ngModel和ngBind添加到自定义指令元素中
- 从控制器访问指令元素
- 如何从指令元素中删除角度观察者
- AngularJS子指令元素选择
- Angular.js指令元素的默认值