从Controller刷新Angular Directive

时间:2016-01-31 19:05:08

标签: javascript angularjs

我搜索了如何从控制器刷新或调用链接功能。我得到了一些解决方案并应用于我的指令,但它不起作用。这是我的指令代码

for (int i = 0; i < array.count; i++) {
    NSCharacterSet *doNotWant = [NSCharacterSet characterSetWithCharactersInString:@" "];
    [array replaceObjectAtIndex:i withObject:[[[array[i] lowercaseString] componentsSeparatedByCharactersInSet: doNotWant] componentsJoinedByString: @"-"]];
}

这是我用来调用指令

的html
(function() {
'use strict';

angular
    .module('app.viewReport')
    .directive("directive", directive)

    function directive() {
        return {
            restrict: "A",
            scope: {
                data: "="
            },
            link: function(scope, ele, attrs) {
                Some Code
            }
        };
    } })();

我想从控制器throguh ajax加载数据时更新指令。这是我的控制器功能。我在ajax中定义了硬编码值,但它也没有用。请帮我。提前致谢

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

1 个答案:

答案 0 :(得分:2)

您需要在数据阵列上放置$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