2015-03-25 123 views
0

我创建这样角运动范围变量指令

.directive('optionLabel', function() { 
    'use strict'; 
    return { 
     replace: true, 
     restrict: 'AE', 
     template: '<div class="error-msg col-xs-12 intelligent-group col-centered"><h1 translate>{{ optionLabel }}</h1></div>' 
    }; 
    }) 

一个指令眼下范围optionLabel在每个使用该指令作为这样的控制器的设置。

$scope.optionLabel = labelService.getOptionLabel(search.searchType); 

我该如何直接在指令中设置它,而不是在5个控制器中重复执行此代码?

回答

1

您可以使用,你有你的范围接入链路:

.directive('optionLabel', function() { 
'use strict'; 
return { 
    replace: true, 
    restrict: 'AE', 
    template: '<div class="error-msg col-xs-12 intelligent-group col-centered"><h1 translate>{{ optionLabel }}</h1></div>', 
link: function(scope, element, attrs) { 
scope.optionLabel = labelService.getOptionLabel(search.searchType); 
}; 
}) 

不要忘了在你的指令注入labelService。

+0

完美谢谢柯莫 – StevieB 2015-03-25 11:56:14