简体   繁体   中英

using $watch in angularjs

I have a form with 2 input fields and requirement is that once user enters valid data into these fields, I need to pass the input data to the factory function and get the data from server.To achieve this I thought of using $watch function but stuck at how to know if form is valid in $wathc function and then call the factory function to get data from the server.Here is the code. Any help would be highly appreciated.

//html

  <html>

       <body ng-app="myModule">
         <div ng-controller="myCtrl">

            Product Id: <input type="text" ng-model="myModel.id" /><br/>
            Product Name: <input type="text" ng-model="myModel.productname" /><br/>

         </div>

       </body>

   </html>

//js

       var myModule = angular.module('myModule',[]);

         myModule.controller('myCtrl',['$scope', function($scope){

                $scope.myModel = {};

                var getdata = function(newVal, oldVal) {

               };


             $scope.$watch('myModel.id', getdata)
             $scope.$watch('myModel.productname', getdata)

         }]);

您不会只看myModel,因为在两种情况下都调用了相同的函数吗?

You could do this with ng-change just as easily.

<html>
   <body ng-app="myModule">
     <form name="productForm" ng-controller="myCtrl">
     <div>

        Product Id: <input type="text" name="idModel" ng-model="myModel.id" ng-change="validateID()" /><br/>
        Product Name: <input type="text" ng-model="myModel.productname" ng-change="validateProduct()" /><br/>

     </div>
    </form>
   </body>

And your JS would look like this:

var myModule = angular.module('myModule',[]);

myModule.controller('myCtrl',['$scope', 'myFactory', 
    function($scope, myFactory){

        $scope.myModel = {};

        $scope.validateID = function(){
            //things that validate the data go here
            if(your data is valid){
                myFactory.get(yourParams).then(function(data){
                    //whatever you need to do with the data goes here
                });
            }
        };

       $scope.validateProduct = function(){
            //things that validate the data go here
            if(your data is valid){
                myFactory.get(yourParams).then(function(data){
                    //whatever you need to do with the data goes here
                });
            }
        };
    }
]);

Using ng-change saves you from having to add a $watch to your scope (they are expensive) and will fire when the user leaves the input box. If you need to catch each keystroke, I would recommend that you use UI-Keypress and run the same functions.

To know if form is valid you have to add a form tag and inside your controller check $valid , on your example the form is always valid becaus you do not have any required field.

See the below example on codepen

The HTML

<div ng-app="myModule">
    <div ng-controller="myCtrl">
        <form name="myform" novalidate>
             Product Id:
            <input type="text" ng-model="myModel.id"  />
            <br/>
             Product Name:
            <input type="text" ng-model="myModel.productname" />
            <br/>
        </form>
        <br/>
        <div>{{result}}</div>
        <div>Form is valid: {{myform.$valid}}</div>
      </div>
    </div>

The JS

var myModule = angular.module('myModule', []);

    myModule.controller('myCtrl', ['$scope', function ($scope) {

        $scope.myModel = {};
        $scope.result = "(null)";
        var getdata = function (newVal, oldVal) {

          var valid = null;

            if ($scope.myform.$valid) {
                valid = "is valid";
            } else {
                valid = "is INVALID";
            }

            $scope.result = "Changed value " + newVal + " form " + valid;
        };

        $scope.$watch('myModel.id', getdata);
        $scope.$watch('myModel.productname', getdata);

    }]);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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