簡體   English   中英

ng-model未在嵌套指令中更新

[英]ng-model is not updating in nested directive

我在指令中嵌入了文本角,該指令具有范圍變量... scope.htmlContent.content。 在指令中我有

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-model?

您需要為http get調用創建一個工廠,如下所示:

//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;
        }
    };
}]);

希望能幫助到你!!

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM