【问题标题】:how to save form data to database in angularjs如何在angularjs中将表单数据保存到数据库
【发布时间】:2017-06-28 20:18:18
【问题描述】:

我在角度方面很新。 我正在开发一个内部工具,我可以在其中获取用户详细信息,例如位置、工作资料和手机号码等。 所以我有一个带有位置、工作资料下拉列表和手机号码输入字段的表单。对于上述字段,我已经通过 ajax get 请求从数据库获取数据。如果我想更改诸如 mobileNumber 之类的数据,我该怎么做以及如何将其保存到应该覆盖之前的手机号码的数据库中。提前致谢。

这是我的html代码:

 <form name="userForm">
                    <div class="page-heading fs-xs-1-5">
                       Update Profile
                    </div>
                    <div class= "second-row">
                      <div class="row">
                        <div class="col-lg-4">
                            <label for="Location"> Location</label>
                            <select class="form-control" placeholder="location" ng-model="user.location" required>
                                    <option value="">Select Location</option>
                                    <option>Hyderabad</option>
                                    <option>Ahmadabad</option>
                                    <option>New Jersey</option>
                                    <option>California</option>
                                    <option>Jaipur</option>
                            </select>
                        </div>
                        <div class="col-lg-4">
                            <label for="WorkProfile"> Work Profile</label>
                            <select class="form-control" placeholder="workprofile" ng-model="user.workProfile" required>
                                    <option value="">Select Profile</option>
                                    <option>UI Developer</option>
                                    <option>Tester</option>
                            </select>
                        </div>
                        <div class="col-lg-4">
                            <label for="ContactNumber"> Contact Number</label>
                            <input class="form-width contact-height" type="text" ng-model="user.mobileNumber" maxlength="20" required></input>
                        </div>
                      </div>   
                    </div>
                    <div class="third-row">
                      <div class="row">
                        <div class="col-lg-4">
                        <label for="SkillSet"> Skill Set</label>
                        <textarea id="SkillSet" class="form-width" placeholder="Enter Your skill set here..." row="4" col="100" ng-model="user.skillset" maxlength="250" required></textarea>
                        </div>
                      </div>
                    </div>        
                    <button class="btn btn-primary setng-btn" type="submit" ng-click="addnewdata">Save</button>
                    </form>

js:`

(function () {

    angular
        .module('app.login')
        .factory('userprofileService', userprofileService);

    userprofileService.$inject = ['config', '$http'];

    function userprofileService (config, $http) {
        var userprofileService = this;
        userprofileService.getUserDetails = function(){
        return $http({
                        method: 'GET',
                        url: config.apiURL+'/v2/user',
                        headers: { 'Content-Type': 'application/json' }
                    }).then(function(response){
                        return response.data;
                    });
        }
`

【问题讨论】:

  • 您向后端发送 PUT 请求,其中包含需要更改的数据,后端会更新数据库。
  • 提交按钮上的ng-click directive需要调用一个函数addnewdata(),而不仅仅是命名函数。

标签: angularjs ajaxform


【解决方案1】:

这太难说清楚了。 简而言之,您需要 REST 服务作为后端来调用以保存数据。

AngularJS 项目只是前端,你需要后端(使用 Nodejs、JSP 或 asp.net/C# 开发)来服务。

【讨论】:

  • 实际上我们在这里使用了一个api,所以我需要将数据发布到那个api,并且在后端api中会将最新的数据保存到数据库中。但在这里我无法将数据发布到 api,你能帮我吗?提前致谢。
猜你喜欢
  • 2019-08-21
  • 2014-11-30
  • 1970-01-01
  • 1970-01-01
  • 2013-03-21
  • 2017-03-06
  • 1970-01-01
  • 2014-08-06
  • 1970-01-01
相关资源
最近更新 更多