[英]Refresh Angular Directive from Controller
我搜索了如何從控制器刷新或調用鏈接功能。 我得到了一些解決方案,並在我的指令上應用了它,但是它不起作用。 這是我的指令代碼
(function() {
'use strict';
angular
.module('app.viewReport')
.directive("directive", directive)
function directive() {
return {
restrict: "A",
scope: {
data: "="
},
link: function(scope, ele, attrs) {
Some Code
}
};
} })();
這是我用來調用指令的html
<div directive
data-data="comboData"
data-type="line"
data-xkey="year"
data-ykeys='["Accpeted", "Bed Assigned", "Patient No-Show"]'
data-labels='["Value A", "Value B", "Value C"]'
data-line-colors='["#8170CA","#E87352","#60CD9B"]'
data-line-width="2"
></div>
當我從控制器throguh ajax加載數據時,我想更新指令。 這是我的控制器功能。 我在ajax中定義了硬編碼的值,但它也不起作用。 請幫我。 提前致謝
$http.get(serviceUrl).success(function (data, status, headers, config) {
$scope.comboData = [{
year: "2008",
"Accpeted": "20",
"Bed Assigned": "16",
"Patient No-Show": "12"
}, {
year: "2009",
"Accpeted": "10",
"Bed Assigned": "22",
"Patient No-Show": "30"
}, {
year: "2010",
"Accpeted": "5",
"Bed Assigned": "14",
"Patient No-Show": "20"
}, {
year: "2011",
"Accpeted": "5",
"Bed Assigned": "12",
"Patient No-Show": "19"
}
];
});
您需要在數據數組上放置一個$watchCollection
。
angular
.module('app.viewReport')
.directive("directive", directive)
function directive() {
return {
restrict: "A",
scope: {
data: "="
},
link: function(scope, ele, attrs) {
scope.$watchCollection("data", function (newArray) {
Some Code
});
}
};
} })();
有關更多信息,請參閱AngularJS $ rootScope.scope API參考-$ watchCollection 。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.