gpt4 book ai didi

javascript - 从 Controller 刷新 Angular 指令

转载 作者:行者123 更新时间:2023-11-30 16:18:28 25 4
gpt4 key购买 nike

我搜索了如何从 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>

当我通过 ajax 从 Controller 加载数据时,我想更新指令。这是我的 Controller 功能。我在 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 Reference -- $watchCollection .


AngularJS的三观深度

-- AngularJS Developer Guide - Scope Watch Depths

关于javascript - 从 Controller 刷新 Angular 指令,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35118063/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com