Javascript 如何在AngularJS中确认特定用户?

Javascript 如何在AngularJS中确认特定用户?,javascript,angularjs,http,get,put,Javascript,Angularjs,Http,Get,Put,所以,我在Angularjs和所有这些新的获取,放置,请求。然而,我正在创建一个应用程序,其中一个页面中有用户列表,第二个页面中有一个带有三个按钮的表单。我关注的是一个“确认”按钮。 所以,我想用确认按钮确认一个名为PUT/users/{userId}/confirm的特定用户。这方面我需要帮助。我已经完成了从后端获取用户的GET部分。但是,无法理解PUT请求 这是到目前为止的代码。 服务 App.js var-app=angular.module(“门户”、“ngRoute”、“ui.boot

所以,我在Angularjs和所有这些新的获取,放置,请求。然而,我正在创建一个应用程序,其中一个页面中有用户列表,第二个页面中有一个带有三个按钮的表单。我关注的是一个“确认”按钮。 所以,我想用确认按钮确认一个名为PUT/users/{userId}/confirm的特定用户。这方面我需要帮助。我已经完成了从后端获取用户的GET部分。但是,无法理解PUT请求

这是到目前为止的代码。 服务

App.js

var-app=angular.module(“门户”、“ngRoute”、“ui.bootstrap”);
应用程序控制器('MyCtrl',函数($scope){
$scope.inactive=true;
$scope.confirmedAction=功能(个人){
$scope.userInfo.users.splice(person.id,1);
location.href='#/user';
person.data=“true”;
console.log(person.data);
控制台日志(个人);
};
});
app.directive('confirmClick',['$q','dialogModal',function($q,dialogModal)
{
返回{
链接:函数(范围、元素、属性){
//ngClick不会等待我们的模式确认窗口解决,
//因此,我们将获取ngClick属性中的其他值
//将在模式解析后继续。
//修改confirmClick()操作,使我们不再执行它
//查找confirmClick()或confirmClick('确定吗?')
var ngClick=attrs.ngClick.replace('confirmClick()','true'))
.replace('confirmClick(','confirmClick(true');
//在作用域上设置确认操作
scope.confirmClick=函数(msg){
//如果msg设置为true,则返回它(这是使对话框正常工作的一种解决方法)
如果(消息===真){
返回true;
}
//消息可以直接传递给confirmClick('您确定要确认吗?')
//在ng中单击
//或通过屏幕上的“确认单击”属性
// 
msg=msg | | | attrs.confirm单击| |“您确定要确认吗?”;
//打开一个对话框,然后在确认后继续单击操作
dialogModal(msg).result.then(函数(){
范围.$eval(ngClick);
});
//返回false以停止当前ng点击流并等待我们的模式答案
返回false;
};
}
}
}])
/*
带有UI引导模式服务的模式确认对话框窗口。
这是一个基本模式,可以显示带有“是”或“否”按钮的消息。
它返回一个承诺,该承诺根据是/否单击被解决或拒绝。
可以传递以下设置:
消息要传递给模态体的消息
标题(可选)模式窗口的标题
“是”按钮的“确定”按钮文本。将“假”设置为不包含按钮
取消没有按钮的按钮文本。ste false不包括按钮
*/
.service('dialogModal',['$modal',function($modal){
返回功能(消息、标题、确认按钮、取消按钮){
//设置按钮的默认值
//如果按钮值设置为false,则不包括该按钮
cancelButton=cancelButton==false?false:(cancelButton | |“No”);
okButton=okButton==false?false:(okButton | | Yes');
//设置控制器以观察单击
var ModalInstanceCtrl=函数($scope、$modalInstance、settings){
//将设置添加到范围
角度扩展($scope,settings);
//单击“是”按钮
$scope.ok=函数(){
$modalInstance.close(true);
};
//没有点击任何按钮
$scope.cancel=函数(){
$modalInstance.disclose('cancel');
};
};
//打开modal并返回实例(这将在单击确定/取消时解析承诺)
var modalInstance=$modal.open({
模板:'\
\
{{modalTitle}}\
\
{{modalBody}}\
\
{{okButton}}\
{{cancelButton}\
\
',
控制器:ModalInstanceCtrl,
决心:{
设置:函数(){
返回{
modalTitle:标题,
modalBody:message,
OK按钮:OK按钮,
取消按钮:取消按钮
};
}
}
});
//返回模态实例
返回模态;
}
}])
//我们的路线
app.config(函数($routeProvider){
$routeProvider
.when(“/user”{
控制器:“家庭控制器”,
templateUrl:“partials/home.html”
})
.when(“/user/:id”{
控制器:“用户控制器”,
templateUrl:“partials/about.html”
})
.否则({
重定向到:'/user'
});
});

返回$http.put('https://admin/v1/users/“+userId+”/confirm',{};
我应该把它放在我的服务中吗?在$http.get下?我会添加一个新方法,
confirmUser:function(userId){}
你能给我看一下吗?另外,我应该把我的app.js放在我有模块的地方吗?我稍后会给你看。我把我的服务分成自己的js文件cleaner,而不是一个巨大的app.js文件
 app.factory('people', ['$http', function($http) {
    var userInfo = {  
    getUserInfo: function () {
   return $http.get('https://admin/v1/unconfirmed_users');


     },




   };
   return userInfo;
  }]);

 app.factory('people', function($http){

 var services = {};
  services.getUserInfo = function() {

    return $http.put('https://users/{userId}/confirm');
  };
 return services;


})
  var app = angular.module("Portal", ['ngRoute',  'ui.bootstrap' ]);


  app.controller('MyCtrl', function($scope) {


     $scope.inactive = true;

     $scope.confirmedAction = function(person) {

     $scope.userInfo.users.splice(person.id, 1);


      location.href = '#/user';



        person.data = "true";
       console.log(person.data);
        console.log(person);





    };

  });


app.directive('confirmClick', ['$q', 'dialogModal', function($q, dialogModal) 
  {
    return {
        link: function (scope, element, attrs) {
            // ngClick won't wait for our modal confirmation window to resolve,
            // so we will grab the other values in the ngClick attribute, which
            // will continue after the modal resolves.
            // modify the confirmClick() action so we don't perform it again
            // looks for either confirmClick() or confirmClick('are you sure?')
            var ngClick = attrs.ngClick.replace('confirmClick()', 'true')
                .replace('confirmClick(', 'confirmClick(true,');

            // setup a confirmation action on the scope
            scope.confirmClick = function(msg) {
                // if the msg was set to true, then return it (this is a workaround to make our dialog work)
                if (msg===true) {
                    return true;
                }
                // msg can be passed directly to confirmClick('Are you sure you want to confirm?')
                // in ng-click
                // or through the confirm-click attribute on the
                // <a confirm-click="Are you sure you want to confirm?"></a>
                msg = msg || attrs.confirmClick || 'Are you sure you want to confirm?';
                // open a dialog modal, and then continue ngClick actions if it's confirmed
                dialogModal(msg).result.then(function() {
                    scope.$eval(ngClick);
                });
                // return false to stop the current ng-click flow and wait for our modal answer
                return false;
            };
        }
    }
}])

/*
 Modal confirmation dialog window with the UI Bootstrap Modal service.
 This is a basic modal that can display a message with yes or no buttons.
 It returns a promise that is resolved or rejected based on yes/no clicks.
 The following settings can be passed:

 message         the message to pass to the modal body
 title           (optional) title for modal window
 okButton        text for YES button. set false to not include button
 cancelButton    text for NO button. ste false to not include button

 */
.service('dialogModal', ['$modal', function($modal) {
    return function (message, title, okButton, cancelButton) {
        // setup default values for buttons
        // if a button value is set to false, then that button won't be included
        cancelButton = cancelButton===false ? false : (cancelButton || 'No');
        okButton = okButton ===false ? false : (okButton || 'Yes');

        // setup the Controller to watch the click
        var ModalInstanceCtrl = function ($scope, $modalInstance, settings) {
            // add settings to scope
            angular.extend($scope, settings);
            // yes button clicked
            $scope.ok = function () {
                $modalInstance.close(true);
            };
            // no button clicked
            $scope.cancel = function () {
                $modalInstance.dismiss('cancel');
            };
        };

        // open modal and return the instance (which will resolve the promise on ok/cancel clicks)
        var modalInstance = $modal.open({
            template: '<div class="dialog-modal"> \
              <div class="modal-header" ng-show="modalTitle"> \
                  <h3 class="modal-title">{{modalTitle}}</h3> \
              </div> \
              <div class="modal-body">{{modalBody}}</div> \
              <div class="modal-footer"> \
                  <button class="btn btn-primary" ng-click="ok()" ng-show="okButton">{{okButton}}</button> \
                  <button class="btn btn-warning" ng-click="cancel()" ng-show="cancelButton">{{cancelButton}}</button> \
              </div> \
          </div>',
            controller: ModalInstanceCtrl,
            resolve: {
                settings: function() {
                    return {
                        modalTitle: title,
                        modalBody: message,
                        okButton: okButton,
                        cancelButton: cancelButton
                    };
                }
            }
        });
        // return the modal instance
        return modalInstance;
    }
}])

 //Our routes
  app.config(function ($routeProvider) {
  $routeProvider
  .when("/user", {
    controller: "HomeController",
    templateUrl: "partials/home.html"
  })
  .when("/user/:id", {
    controller: "UserController",
    templateUrl: "partials/about.html"

})
.otherwise({
    redirectTo: '/user'

  });

  });
app.factory('people', function ($http) {
    var service = {};
    service.getUserInfo = function () {
        return $http.get('https://admin/v1/unconfirmed_users');
    };
    service.confirmUser = function (userId) {
        return $http.put('https://users/' + userId + '/confirm', {});
    };
    return service;
});