2017-07-17 19 views
0

我想在我的指令中訪問標題的完整值。 指令中的標題應該顯示爲「Hi from Sample」。 現在它是「你好」。 有人也可以解釋爲什麼會發生?如何在angular 1.5中訪問指令中的屬性文字值?

角代碼

angular.module('docsTransclusionExample', []) 
.controller('Controller', ['$scope', function($scope) { 
    $scope.title = 'Sample'; 
}]) 
.directive('myDialog', function() { 
    return { 
    restrict: 'E', 
    replace: true, 
    transclude: true, 
    scope: {}, 
    template: '<div>'+attr.title'</div>', 
    link: function(scope,title) { 
     scope.title = attr.title; 
    } 
    }; 
}); 

HTML

<my-dialog title="Hi from {{title}}"><h2>Hello</h2> 
</my-dialog> 

回答

0

我計算出來。我正在爲面臨同樣問題的任何人發佈答案。

我在鏈接函數中的一個觀察器中賦值。

scope.$watch('somevalue', function(value){ 
    if(value == true){ 
    scope.title = attrs.title; 
    } 
}); 

注意:somevalue可以是任何變化的變化。對我而言,這是模型中的一個價值。

相關問題