2015-03-25 120 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