ngMessages在指令模板中不起作用

时间:2015-07-22 06:45:02

标签: angularjs angularjs-directive ng-messages

我的ngMessages在我的指令模板中不起作用!

我有一个带有模板和链接函数的指令myInput,在模板函数中我为包装的<label><input>创建模板字符串。

在Link函数中,我使用require: '^form' FormController并检索表单名称。然后我在包裹的元素之后放置一个ngMessages块。

    (function () {
        'use strict';

        angular
            .module('app.components')
            .directive('myInput', MyInput);

        /*@ngInject*/
        function MyInput($compile, ValidatorService, _, LIST_OF_VALIDATORS) {
            return {
                require: '^form',
                restrict: 'E',
                controller: MyInputController,
                controllerAs: 'vm',
                bindToController: true,
                template: TemplateFunction,
                scope: {
                    label: '@',
                    id: '@',
                    value: '=',
                    validateCustom: '&'
                },
                 link: MyInputLink

            };

            function MyInputController($attrs) {
                var vm = this;
                vm.value = '';
                vm.validateClass = '';
                vm.successMessage = '';
                vm.errorMessage = '';
            }

            function TemplateFunction(tElement, tAttrs) {
                return '<div class="input-field">' +
                    '   <label id="input_{{vm.id}}_label" for="input_{{vm.id}}" >{{vm.label}}</label>' +
                    '   <input id="input_{{vm.id}}" name="{{vm.id}}" ng-class="vm.validateClass" type="text" ng-model="vm.value" >' +
                    '</div>';

            }

            function MyInputLink(scope, element, attrs, form){
                var extra = '   <div ng-messages="' + form.$name + '.' + scope.vm.id + '.$error">' +
                    '       <div ng-messages-include="/modules/components/validationMessages.html"></div>' +
                    '   </div>';
                $(element).after(extra);
            }
        }
    })();

用法:

    <h1>Test</h1>
    <form name="myForm">
        <my-input label="My Input" id="input1" value="vm.input1"></my-input>

        -------

        <!-- this block is hardcoded and is working, it does not come from the directive! -->
        <div ng-messages="myForm.input1.$error">
            <div ng-messages-include="/modules/components/validationMessages.html"></div>
        </div>

    </form>

2 个答案:

答案 0 :(得分:0)

不是在link函数中添加ngMessages块,而是将其添加到compile函数中。

由于缺少link,它不像FormController函数那样方便,但它有效:-)

这里是代码:

    compile: function(tElement, tAttrs){
        var name = tElement.closest('form').attr('name'),
                   fullFieldName = name + '.' + tAttrs.id; // or tAttrs.name
        var extra = '<div ng-messages="' + fullFieldName + '.$error">' +
                    '    <div ng-messages-include="/modules/components/validationMessages.html"></div>' +
                    '</div>';
        $(element).after(extra);

答案 1 :(得分:0)

以下是我所做的,我添加到范围,myForm: '='然后在指令<div ng-messages="vm.myForm[vm.id].$error" >

的模板中

我觉得这比链接功能更清晰。