我能将class属性传递给angularJS中的指令模板吗?

msq*_*qar 5 javascript angularjs

我在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);
        }
    }
});
Run Code Online (Sandbox Code Playgroud)

所以我可以从这样的视图中调用它:

<notification type="alert.type" message="alert.msg"></notification>
Run Code Online (Sandbox Code Playgroud)

在控制器中我定义了警报对象:

$scope.alert = { type : 'success', msg : 'This is a test'};
Run Code Online (Sandbox Code Playgroud)

我怎么能动态传递这个类型?尝试过,它没有用.如果我将警报成功传递给指令,它可以工作,但我希望它是动态的.

我能够通过这样做动态传递它吗?

谢谢

Max*_*tin 5

尝试将指令更改为此:

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);
        }
    }
});
Run Code Online (Sandbox Code Playgroud)

由于您具有隔离范围,因此可以使用=绑定父范围属性

演示Fiddle