更改对象声明后,Angularjs Custom指令不起作用

时间:2015-03-29 12:19:34

标签: javascript angularjs angularjs-directive

我在我的项目中添加了自定义validation-summary指令from this git-hub link.它运行正常。

angular.module("ValidationSummaryModule").directive("validationSummary", function() {
    return {
        restrict: "A",
        require: "^form",
        template: "<ul><li ng-repeat='(expression,message) in validationMessages'>{{message}}</li></ul>",
        link: function(scope, element, attributes, controller) {

            scope.validationMessages = {};

            // Hooks up a watch using [ng-show] expression
            controller.watchValidation = function(expression, message) {

                // watch the return value from the scope.$eval(expression)
                scope.$watch(function() { return scope.$eval(expression); }, function(isVisible) {

                    // check if the validation message exists
                    var containsMessage = scope.validationMessages.hasOwnProperty(expression);

                    // if the validation message doesn't exist and it should be visible, add it to the list
                    if(!containsMessage && isVisible)
                        scope.validationMessages[expression] = message;

                    // if the validation message does exist and it shouldn't be visible, delete it
                    if(containsMessage && !isVisible)
                        delete scope.validationMessages[expression];
                });

            };

        }
    };
}); 

但是,如果我将对象声明从scope.validationMessages = {};更改为scope.validationMessages = [];它不起作用。我在浏览器中调试了代码,但找不到原因。

我需要更改声明,因为scope.validationMessages = {};给了我一个无序列表。我需要有序清单。

0 个答案:

没有答案