Restangular POST form data in json format in Angular JS

喜欢而已 提交于 2019-12-30 05:40:09

问题


I am new to Angular JS and I am in a need to use restangular POST of form data in json format. Also I am new to the POST functionalities, can anyone tell me how I can do it??

MY Index Page:

<form ng-controller="registerCtrl">
 <input placeholder="Username" type="email" name="username"  ng-model="user.email" />
 <input placeholder="Password" type="password" name="password" ng-model="user.password"/>
 <input placeholder="Confirm Password" type="password" name="confirmpassword"  ng-model="user.confirmPassword" />
 <button class="btn btn-info" ng-click="registerUser()" type="submit">Login</button>
</form>

Controller

var myApp=angular.module('myApp',['restangular']);
function registerCtrl($scope, Restangular){
$scope.user = {};
$scope.registerUser=function(){
        $scope.people = Restangular.all('data.json/:user').post($scope.user);
    }
}

Here where should I pass the input values as the Json format..... If am wrong with the code, pls correct me.....


回答1:


In Restangular posts should be done to collections not elements.

In your case POST should be made like this;

$scope.user = {email :"", password: "", confpass: ""};

Then make POST request like;

Restangular.all('data.json/:user').post("users", $scope.user);

Reference;

https://github.com/mgonto/restangular#element-methods



来源:https://stackoverflow.com/questions/18585010/restangular-post-form-data-in-json-format-in-angular-js

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!