Home  >  Q&A  >  body text

angular.js - 在书上看到这么一段“自定义验证用户名是否已存在”的代码,但是看不懂,求教

url和data部分什么意思?我该如何修改


    .directive('ensureUnique', function ($http) {
            return {
                require: 'ngModel',
                link: function (scope, elem, attrs, ctrl) {
                   scope.$watch(attrs.ngModel,function(){
                       $http({method:'POST',
                        url:'/api/check/'+attrs.ensureUnique,
                           data:{field:attrs.ensureUnique,valud:scope.ngModel}
                       }).success(function(data,status,header,cfg){
                           ctrl.$setValidity('unique',data.isUnique);
                       }).error(function(data,status,header,cfg){
                           ctrl.$setValidity('unique',false);
                       })
                   })
                }
            }
        })
为情所困为情所困2713 days ago541

reply all(1)I'll reply

  • PHP中文网

    PHP中文网2017-05-15 16:59:50

    It depends on how your post interacts with the server.
    The basic principle is to post the data you need, that is, the data part, to the server. On the server, you check whether the uploaded data is legal and whether it indicates that the user already exists, and then return a data structure, such as json, etc. In the success function, the returned data will be stored in the data parameter, and the final judgment is completed by checking the data in data.

    reply
    0
  • Cancelreply