[英]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.