search

Home  >  Q&A  >  body text

angular.js - I saw this piece of code in a book that "customizes to verify whether the username already exists", but I don't understand it. Please give me some advice.

What do the url and data parts mean? How should I modify it


    .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);
                       })
                   })
                }
            }
        })
为情所困为情所困2785 days ago586

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