Skip to content
This repository was archived by the owner on Sep 8, 2020. It is now read-only.
This repository was archived by the owner on Sep 8, 2020. It is now read-only.

After second trigger of the validation the model binding value is undefined #20

@marcelinojorgeromero

Description

@marcelinojorgeromero

I'm using a directive for an input text control where i have the following two methods inside controller function.
The first time i paste some text in the text box both methods are triggered. When i type in or delete a character from the text box first is called the validation method and then the ng-change associated method (tiggeredOnTextInputChange) but the value passed (val) is undefined (ng-model="theBindingModelValue"). How can i deal with this?

        $scope.tiggeredOnTextInputChange = function (val) {
            var processedVal = val.length - 1; //"val" will be undefined the second time ngChange is triggered so this line will throw error
            $scope.theBindingModelValue = processedVal;
        };
        $scope.validateUserInput = function(value){
            var isValid = someFactoryWithoutAsyncCalls.validate(value);
            return isValid;
        }

Html of the directive (testInput.html)

<div class="form-group" data-ng-form name="myForm">
  <input type="text" name="{{inputName}}" placeholder={{inputPlaceholder}} class="form-control" data-ng-model="theBindingModelValue" data-ng-change="tiggeredOnTextInputChange(theBindingModelValue)" ui-validate="{isInputValid:'validateUserInput($value)'}"/>
  <span data-ng-show="myForm['\{\{inputName\}\}'].$error.isInputValid">Invalid input</span>
</div>

The complete directive:

function testInput() {

    function testInputCtrl($scope, someFactoryWithoutAsyncCalls) {
        $scope.tiggeredOnTextInputChange = function (val) {
            var processedVal = val.length - 1; //"val" will be undefined the second time ngChange is triggered so this line will throw error
            $scope.theBindingModelValue = processedVal;
        };
        $scope.validateUserInput = function(value){
            var isValid = someFactoryWithoutAsyncCalls.validate(value);
            return isValid;
        }
    }

    testInputCtrl.$inject = ["$scope", "someFactoryWithoutAsyncCalls"];

    return {
        scope: {
            inputName: "@",
            inputPlaceholder: "@"
        },
        restrict: "EA",
        require: "ngModel",
        replace: true,
        templateUrl: "testInput.html",
        controller: testInputCtrl
    };
}

I call it this way:

<test-input input-name="testinput" input-placeholder="TEST"></test-input>

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions