有以下問題。如何使用外部數據更新ng-model?
考慮,我們有以下的HTML代碼:
<div id="container" ng-controller="Controller">
<my-tag ng-model="values"></my-tag>
</div>
而下面的控制器:
var Controller = myApp.controller("Controller", function ($scope, $http, $filter, $q) {
$scope.values = [];
$http.get(someURLHere).then(function(response) {
var data = JSON.parse(response.data);
$scope.values = data;
});
/* And so on ... */
});
,這裏是指令聲明:
myTag.directive('myTag', function() {
return {
require: "^ngModel",
restrict: "E",
replace: true,
scope: {
ngModel : "=",
},
controller: ['$scope', '$filter', myTagController],
templateUrl: /* Here is a path to my template*/,
link: function (scope, elem, attrs, ctrl) {
scope.data = scope.ngModel;
}
}
});
標籤「我的 - 標籤「是我的自定義指令。它通過ng-model獲取一些數據並呈現它們。 而ng-controller="Controller"
是一些使用AJAX檢索某些數據的函數,它假設將它們寫入$scope.values
。之後,我預計他們將通過ng-model="values"
發送到my-tag
。接下來,假設數據在HTML頁面上呈現。但他們不! 我相信我的數據已在Controller
中正確檢索,但在my-tag
中更新ng模型時出現問題。 將Controller
中檢索的數據「發送」到我的自定義指令的ng模型的最佳方法是什麼?
請添加您的指令代碼 – Sprottenwels