0

我制作了一个嵌套指令,并尝试从 html 视图中调用它,但没有更新 html 中的视图元素。

我可以从调用控制器获取更新的值,但在视图级别看不到更新的效果。

这里是 Js

    // Code goes here
"use strict";
var myApp=angular.module("myApp",[]);

myApp.directive("selectDirective", [function () {
    return {
        restrict: "E",
        template: '<select class="form-control input-sm dropdown" data-ng-model="model.args.selectedItem" data-ng-options="item[model.args.displayField] for item in model.args.source" data-ng-change="model.itemChange(model.args.selectedItem)"><option value="">Select Any Item</option></select>',
        scope: {
        },
        bindToController: { args: "=" },
        link: function (scope, element, attrs) {
            var self = scope.model || {};
            var initializeControl = function () {
                if (self.args == undefined) {
                    self.args = {};
                }
                if (self.args.method == undefined) {
                    self.args.method = {};
                }
                if (self.args.isDisabled == undefined) {
                    self.args.isDisabled = false;
                }
                if (self.args.displayField == undefined) {
                    self.args.displayField = '';
                    //alert('Display Field is blank for dropdown control.')
                }
                if (self.args.valueField == undefined) {
                    self.args.valueField = '';
                    //alert('Value Field is blank for dropdown control.')
                }
                if (self.args.source == undefined) {
                    self.args.source = {};
                }
                if (self.args.hide == undefined) {
                    self.args.hide = false;
                }
            }
            var assignMethod = function () {
                self.args.method =
                    {
                        setEnable: function (args) {
                            self.args.isDisabled = !args;
                        },
                        setVisible: function (args) {
                            self.args.hide = !args;
                        },
                        getText: function () {
                            return self.args.selectedText;
                        },
                        getValue: function () {
                            return self.args.selectedValue;
                        },
                        setItem: function (item) {
                            if (item != undefined) {
                                var index = self.args.source.indexOf(item);
                                self.args.selectedText = item[self.args.displayField];
                                self.args.selectedValue = item[self.args.valueField];
                                self.args.selectedItem = item;
                                self.args.selectedIndex = index;
                            }

                        }
                    }
            }
            self.itemChange = function (item) {

                if (item != undefined) {
                    var index = self.args.source.indexOf(item);
                    self.args.selectedText = item[self.args.displayField];
                    self.args.selectedValue = item[self.args.valueField];
                    self.args.selectedItem = item;
                    self.args.selectedIndex = index;
                }
            }

            initializeControl();
            assignMethod();

        },
        controller: function () { },
        controllerAs: 'model'
    }
}]);
myApp.directive("stateDirective", [function () {
    return {
        restrict: "E",
        template: '<select-directive args="model.args"></select-directive>',
        scope: {},
        bindToController: { args: "=" },
        link: function (scope, element, attrs) {
            var self = scope.model || {};

            var initializeControl = function () {
                if (self.args == undefined) {
                    self.args = {};
                }
                var sourceList = [{ id: 1, name: "AA", value: "AA" },
                              { id: 2, name: "AB", value: "AB" },
                              { id: 3, name: "ABD", value: "ABD" },
                              { id: 4, name: "ABE", value: "ABE" },
                              { id: 5, name: "ACT", value: "ACT" },
                              { id: 6, name: "AE", value: "AE" }];
                self.args.source = sourceList;
                self.args.displayField = 'name';
                self.args.valueField = 'value';
            }();

        },
        controller: function () {

        },
        controllerAs: 'model'
    }
}]);
myApp.controller("homeController", ['$interval', function ($interval) {
    var self = this;

    var initializeControl = function () {

        self.state1 = {};
        self.state2 = {};

        self.ClickMe = function () {
            debugger;
            aler(1);
            self.state2.method.setItem(self.state1.selectedItem);
        }

        self.ClickMe2 = function () {
            debugger;
             aler(1);
            var x1 = self.state1;
            var x2 = self.state2;
        }



    };
    $interval(function () {
    }, 500);

    initializeControl();
}]);

这是HTML:

     <div class="cold-md-12" ng-controller="homeController as model">
    <h1>Home Page</h1>
    <state-directive args="model.state1"></state-directive>
    <br />
    <input type="button" value="ClickMe" data-ng-click="model.ClickMe()" />
    <state-directive args="model.state2"></state-directive><br />
    <input type="button" value="Submit" data-ng-click="model.ClickMe2()" />
</div>

小提琴在这里。

注意:我也尝试在指令和控制器中使用$watchand$timeout但没有奏效。

4

2 回答 2

0

刚加track by进去ng-option

我只是将data-ng-options值从

item[model.args.displayField] for item in model.args.source

item[model.args.displayField] for item in model.args.source track by item[model.args.valueField] 

它运行良好。

于 2017-01-20T20:36:22.180 回答
0

如果scope.model未初始化,您将在链接功能中分配{}给。self但是你从来没有真正把它放在控制器上。如果scope.model存在,那很好,因为然后您将所有内容绑定到控制器,该控制器绑定到范围,但如果不是,则出现问题。我猜这就是正在发生的事情。

虽然,如果我可能如此大胆,我会说命名您的控制器模型非常具有误导性。因为在开发世界中,模型和控制器是两个非常不同的东西。

干杯,亚伦

于 2017-01-12T22:51:51.233 回答