0

我將文本角度嵌入到具有範圍變量的指令中... scope.htmlContent.content。在指令中我有ng-model在嵌套指令中沒有更新

template: 
''' 
// This updates just fine. I use it to debug so I will take this out from time to time 
<p ng-bind='htmlContent.content'></p> 
// ng-model htmlContent.content stays blank and does not update 
<text-angular ng-model='htmlContent.content'> 
</text-angular> 
''', 
link: function(scope, ele, attr, ctrl) { 
//some code 
$http({ 
    method: 'GET' 
    url: 'someurl.com' 
}).success(function(data,headers,config) { 
    // This does not update text-angular 
    scope.htmlContent.content = data; 
    // If I add this, it will error out 
    scope.$apply() 
}) 
} 

無論如何,ng-model沒有正確更新。只有當我明確地在某些異步fxn的鏈接函數的開始部分設置scope.htmlContent.content時,它才起作用。我怎樣才能更新ng模型?

+1

爲http調用和tgen jnject創建一個工廠,使其進入指令 – V31

+0

如果您更多地解釋了這一點並將其添加爲答案,我會接受它。 –

+0

你在範圍上遇到什麼錯誤。$ apply? – V31

回答

1

您需要爲您的HTTP創建一個工廠得到調用是這樣的:

//Please change it as per your needs 
app.factory('factoryProvider', function(){ 
    return { 
    yourData:function(callback){ 
     $http.get('url').success(callback); 
    } 
    } 
}); 

然後在你的指令,你需要注入工廠

app.directive('myDiv',['factoryProvider', function(factoryProvider) { 
    return { 
     restrict: 'E', 
     replace: true, 
     template: '<p>{{name}}</p>', 
     controller: function($scope) { 
     }, 
     link: function(scope) { 
      scope.data=factoryProvider.yourData; 
     } 
    }; 
}]); 

希望它可以幫助!

+0

爲什麼ngModel無法實時更新? –

+0

指令有其自己的隔離範圍。您是否按照指令的要求製作了ngModel? – V31

+0

我試過了,我是否必須對transclution或指令控制器做些有趣的事情?我得到了孤立範圍的概念,但我不太確定這與ngModel有什麼關係。 –