如何使用无效和原始在angularjs中进行验证?还有如何在angular js中进行服务调用 [英] How to do validation in angularjs using invalid and pristine? Also how to do service calls in angular js

查看:28
本文介绍了如何使用无效和原始在angularjs中进行验证?还有如何在angular js中进行服务调用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

angularjs页面中无效,原始等的用途是什么.如何使用这些进行表单验证?

What is the use of invalid, pristine, etc in angularjs page. How to use these for form validation?

推荐答案

要求1:

在服务文件中

app.factory('CrusdService', function($http) {
  return {
    fetchAll: function() {
      return $http.get('https:\\localHost:5000\countries').then(
        function(response) {
          return response.data.data;
        },
        function(error) {
          return error;
        }
      );
    },
    add: function(data) {
      return $http.post('https:\\localHost:5000\country', data).then(
        function(response) {
          return response;
        },
        function(error) {
          console.log('error');
        }
      );
    },
    update: function(data) {
      var name = {
        "name": data.name
      };
      return $http.put('https:\\localHost:5000\country' + data._id, name).then(
        function(response) {
          return response;
        },
        function(error) {
          console.log('error');
        }
      );
    },
    activate: function(id) {
      return $http.put('https:\\localHost:5000\country' + id + '\activate').then(
        function(response) {
          return response;
        },
        function(error) {
          console.log('error');
        }
      );
    },
    deactivate: function(id) {
      return $http.put('https:\\localHost:5000\country' + id + '\deactivate').then(
        function(response) {
          return response;
        },
        function(error) {
          console.log('error');
        }
      );
    }
  }
});

在控制器文件中

function countryList() {
  CrudeService.fecthAll().then(
    function(data) {
      $scope.countries = data;
    },
    function(data) {
      console.log('error');
    }
  );
}
countryList();

// insert within the method given for add country
CrudeService.add($scope.country).then(
  function(data) {
    countryList();
  },
  function(data) {
    console.log('error');
  }
);

// insert within the method given for update country
CrudeService.update($scope.country).then(
  function(data) {
    countryList();
  },
  function(data) {
    console.log('error');
  }
);

// insert within the method given for activate country
CrudeService.activate(itemsId).then(
  function(data) {
    countryList();
  },
  function(data) {
    console.log('error');
  }
);

// insert within the method given for deactivate country
CrudeService.deactivate(itemsId).then(
  function(data) {
    countryList();
  },
  function(data) {
    console.log('error');
  }
);


要求2:

使用搜索框过滤表数据


Requirement 2:

filter table data using search box

在html文件中添加以下内容:

in html file add the following content:

对于搜索字段,请提供 ng-model ="searchValue"
ng-repeat =国家/地区的数据| .. | filter:searchValue"

For the search field give the ng-model="searchValue"
In ng-repeat = "data in country |.. | filter:searchValue"

验证和错误消息

在html文件中添加以下内容:

in html file add the following content:

<span class="error" ng-if="formname.inputname.$invalid">enter correct data</span>

用于取消弹出式窗口中的保存和更新"按钮

for disbaling save and update button in pop up

保存- ng-disabled ="formname.inputname.$ invalid || formname.inputname.$ pristine"
更新- ng-disabled ="formname.inputname.$ invalid || formname.inputname.$ pristine"

这篇关于如何使用无效和原始在angularjs中进行验证?还有如何在angular js中进行服务调用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆