简体   繁体   English

AngularJS监视未触发

[英]AngularJS watch not being triggered

I'm having a problem with AngularJs. 我在AngularJs上遇到问题。 I have created a directive that $watch the model and it takes some action based on the model's current status. 我创建了一个指令$ watch模型,它根据模型的当前状态采取一些措施。 However, although during debugging I can see that the $watch is set, it is only being triggered after the model get valid at least once and I don't know why that is happening. 但是,尽管在调试过程中我可以看到$ watch已设置,但是它仅在模型至少有效一次之后才被​​触发,而且我不知道为什么会这样。 Debugging it doesn't even gets into the $watch function when something is typed. 键入内容时,调试它甚至都没有进入$ watch函数。

The code is below: 代码如下:

Directive: 指示:

(function() {
    'use strict';

    var app = angular.module('app');

    app.directive('tooltipValidation', function () {


        return {
            restrict: 'A',
            require: 'ngModel',
            link: function (scope, element, attrs, ngModel) {

                var tooltip = $(element).qtip({
                    content: {
                        text: element.next('div')
                    },
                    show: false,
                    hide: true
                }).qtip('api');

                scope.$watch(attrs.ngModel, function() {
                    if (ngModel.$invalid && ngModel.$dirty) {
                        tooltip.show();
                    } else {
                        tooltip.hide();
                    }
                });
            }
        };
    });
})()

HTML: HTML:

<div class="form-address clearfix" ng-show="isNewShippingAddress" ng-form="newShippingAddressForm">                                    
    <h3>Include new shipping address:</h3>
    <div class="novo-endereco clearfix" id="newAddress">
        <div class="required address apelido">
            <label for="newShippingAddressAlias">Alias</label>
            <input id="newShippingAddressAlias" name="newShippingAddressAlias" type="text" tooltip-validation ng-model="newShippingAddress.Alias" required ng-maxlength="32" />
            <div data-ng-show="newShippingAddressForm.newShippingAddressAlias.$dirty && newShippingAddressForm.newShippingAddressAlias.$invalid">
                <p data-ng-show="newShippingAddressForm.newShippingAddressAlias.$error.required">obligatory</p>
                <p data-ng-show="newShippingAddressForm.newShippingAddressAlias.$error.maxlength">max 32 char</p>
            </div>
        </div>
        <div class="required endereco">
            <label for="newShippingAddressStreet">Street</label>
            <input id="newShippingAddressStreet" name="newShippingAddressStreet" type="text" tooltip-validation ng-model="newShippingAddress.Street" required ng-maxlength="256" />
            <div data-ng-show="newShippingAddressForm.newShippingAddressStreet.$dirty && newShippingAddressForm.newShippingAddressStreet.$invalid">
                <p data-ng-show="newShippingAddressForm.newShippingAddressStreet.$error.required">obligatory</p>
                <p data-ng-show="newShippingAddressForm.newShippingAddressStreet.$error.maxlength">max 256 char</p>
            </div>
        </div>
        <div class="required cep">
            <label for="newShippingAddressZipCode">ZipCode</label>
            <input id="newShippingAddressZipCode" name="newShippingAddressZipCode" type="text" tooltip-validation ng-model="newShippingAddress.ZipCode" required ng-pattern="/^[0-9]{8}$/" />
            <div data-ng-show="newShippingAddressForm.newShippingAddressZipCode.$dirty && newShippingAddressForm.newShippingAddressZipCode.$invalid">
                <p data-ng-show="newShippingAddressForm.newShippingAddressZipCode.$error.required">obligatory</p>
                <p data-ng-show="newShippingAddressForm.newShippingAddressZipCode.$error.pattern">8 digits</p>
            </div>
        </div>
        <input type="submit" class="button grey" value="Save new address" data-ng-click="saveShippingAddress()" ng-disabled="newShippingAddressForm.$invalid" />
    </div>
</div>

Regards, 问候,

dimello dimello

Try: 尝试:

scope.$watch(function(){
            return ngModel.$viewValue; //Watch for view value (the value in your input)
          }, function() {
           if (ngModel.$invalid && ngModel.$dirty) {
                 tooltip.show();
           } else {
                 tooltip.hide();
           }
   });

DEMO DEMO

Explanation : 说明

When you type an invalid value into the input with ng-model, the underlying model is not updated , causing your scope.$watch(attrs.ngModel not being fired because you're watching for changes in the model. If you need to fire the function every time the input changes no matter it's valid or not, try the above solution. 当您使用ng-model在输入中键入无效值时,基础模型不会更新 ,从而导致您的scope.$watch(attrs.ngModel不会被触发,因为您正在监视模型中的更改。如果需要触发每次输入更改时该函数是否有效,请尝试上述解决方案。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM