admin管理员组文章数量:1323715
I got this directive in angularJS
productApp.directive('notification', function($timeout) {
return {
restrict : 'E',
replace : true,
scope : {
type: "@",
message: "@"
},
template : '<alert class="alert alert-type">message</alert>',
link : function(scope, element, attrs) {
$timeout(function() {
element.hide();
}, 3000);
}
}
});
So i can call it from the view like this:
<notification type="alert.type" message="alert.msg"></notification>
In the controller i got the alert object defined:
$scope.alert = { type : 'success', msg : 'This is a test'};
How am i able to pass the type dynamically? tried that and it didn't work. If i pass alert-success to the directive, it works, but i want it to be dynamic.
Am i able to pass it dynamically by doing that?
Thanks
I got this directive in angularJS
productApp.directive('notification', function($timeout) {
return {
restrict : 'E',
replace : true,
scope : {
type: "@",
message: "@"
},
template : '<alert class="alert alert-type">message</alert>',
link : function(scope, element, attrs) {
$timeout(function() {
element.hide();
}, 3000);
}
}
});
So i can call it from the view like this:
<notification type="alert.type" message="alert.msg"></notification>
In the controller i got the alert object defined:
$scope.alert = { type : 'success', msg : 'This is a test'};
How am i able to pass the type dynamically? tried that and it didn't work. If i pass alert-success to the directive, it works, but i want it to be dynamic.
Am i able to pass it dynamically by doing that?
Thanks
Share Improve this question asked Dec 11, 2013 at 20:20 msqarmsqar 3,0406 gold badges52 silver badges99 bronze badges2 Answers
Reset to default 5Try to change directive to this one:
productApp.directive('notification', function($timeout) {
return {
restrict : 'E',
replace : true,
scope : {
type: "=",
message: "="
},
template : '<alert class="alert alert-{{type}}">{{message}}</alert>',
link : function(scope, element, attrs) {
$timeout(function() {
// element.hide();
}, 3000);
}
}
});
Since you have isolated scope use =
to bind parent scope property
Demo Fiddle
In your link function, you can do something like this
attrs.type
and attrs.msg
to retrieve the value you pass to your directive.
本文标签: javascriptAm I able to pass a class attribute to a directive template in angularJSStack Overflow
版权声明:本文标题:javascript - Am I able to pass a class attribute to a directive template in angularJS? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742125915a2421939.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论