【问题标题】:How to get back data from DB after sent it from client side?从客户端发送数据后如何从数据库取回数据?
【发布时间】:2016-06-14 19:04:30
【问题描述】:

在我填写并将它们发送到 DB 之后,我在 angular js 中有个人资料页面。然后再一次,如果我回到同一个个人资料页面,我在该字段中没有得到任何值。

profile.html

<form name="profileForm">
            <br/>

                <div flex style="max-width:700px;">
                    <md-input-container class="md-block">
                    <label>Name</label>
                    <input ng-model="vm.name"></input>
                </md-input-container>
                </div>

                <div flex style="max-width:700px;">
                    <md-input-container class="md-block">
                        <label>Mobile Number</label>
                        <input ng-model="vm.mobile"></input>
                    </md-input-container>
                </div>

                <div flex style="max-width:700px;">
                    <md-input-container class="md-block">
                        <label>Email</label>
                        <input ng-model="vm.email"></input>
                    </md-input-container>
                </div>  

                <div flex style="max-width:700px;">
                <label>DOB:</label>
                    <md-datepicker ng-model="vm.profileForm.dob" md-placeholder="Enter date">
                    </md-datepicker>
                </div>

                <div flex style="max-width:700px;">
                    <md-input-container class="md-block" >
                        <label>Country</label>
                        <md-select ng-model="vm.profileForm.Country" ng-change="vm.getCountryStates()">
                            <md-option ng-repeat="countryName in vm.countries" value="{{countryName.id}}" >
                                {{countryName.country}}
                            </md-option>
                        </md-select>
                    </md-input-container>
                </div>

                <div flex style="max-width:700px;">
                    <md-input-container class="md-block" >
                        <label>State</label>
                        <md-select ng-model="vm.profileForm.State"  ng-change = "vm.getStateCities()">
                            <md-option ng-repeat="stateName in vm.states" value="{{stateName.Id}}">
                                {{stateName.state}}
                            </md-option>
                        </md-select>
                    </md-input-container>
                </div>

                <div flex style="max-width:700px;">
                    <md-input-container class="md-block" >
                        <label>City</label>
                        <md-select ng-model="vm.profileForm.City">
                            <md-option ng-repeat="cityName in vm.cities" value="{{cityName.Id}}">
                                {{cityName.city}}
                            </md-option>
                        </md-select>
                    </md-input-container>
                </div>

                <div flex style="max-width:700px;">
                    <md-input-container class="md-block" >
                        <label>Postal Code</label>
                        <input name="postalCode" ng-model="vm.profileForm.postalCode" ng-pattern="/^[0-9]{6}$/" md-maxlength="6">
                    </md-input-container>
                </div>

                <span style="color:green; font-size:20px;">{{vm.message}}</span>

                <div layout="row" layout-align="space-between center">
                    <div>
                        <img src="assets/icons/fonts/backArrow.png" alt="image caption" style="width:18px; height:18px">
                        <a ng-href ng-click="vm.nextTab()" >Back</a>
                    </div>
                    <div style="margin-right:300px">
                        <a ng-href="http://localhost:3000/pages/dashboard">Skip</a>
                        <md-button class="md-raised md-primary" ng-click="vm.profileInfo(vm.profileForm);">Save & Continue</md-button>
                    </div>
                </div>
            </form>

在图片中,通过 ngStorage 获取的前 3 个值,在填充了存储在 DB 中的剩余字段数据后。对于 chekcBox,我使用了级联下拉菜单。

profile.js

(function ()
 {
'use strict';

angular
    .module('app.invite-friends')
    .controller('InviteFriendsController', InviteFriendsController);

/** @ngInject */
function InviteFriendsController(Friendslist, $localStorage, $scope, $http, $location, CustomerService)
{
    var vm = this;

    vm.countries = CustomerService.getCountry();

    vm.getCountryStates = function(){
        vm.states = CustomerService.getCountryState(vm.profileForm.Country);    
        vm.cities =[];
    }

    vm.getStateCities = function(){
        vm.cities = CustomerService.getStateCity(vm.profileForm.State);
    }

    vm.uid = $localStorage._id;

    vm.profileInfo = function(userData){
        $http({
            url:'http://192.168.2.8:7200/api/profile_save',
            method:'POST',
            data: {info:userData, loginId:vm.uid}
        }).then(function(res){
            if(res.data.success){
                vm.message = 'Your profile information has been saved successfully.';
                $location.path('/pages/dashboard')
            } 
        }, function(error){
            alert(error.data);
        });
    };

    $http({
        url:'http://192.168.2.8:7200/api/checkIfExists',
        method:'POST',
        data: {userId: vm.uid}
    }).success(function(res){
        vm.name = res.result[0].name;
        vm.mobile = res.result[0].mobile;
        vm.email = res.result[0].email;
        vm.DOB = res.result[0].DOB;
        vm.Country = res.result[0].Country;
        vm.State = res.result[0].State;
        vm.City = res.result[0].City;
        vm.postalCode = res.result[0].PostCode;
    }, function(error){
        alert(error.data);
    });
     }
   })();

【问题讨论】:

  • 你能贴出你的角码吗?
  • 有代码做数据检索吗?如果是这样,您可以发布它以便我们调试吗?如果没有,则必须进行数据检索,以便您可以显示它。
  • 是的,我在我的问题中添加了角度代码。

标签: angularjs node.js mongodb


【解决方案1】:

所有你的 ng-model 都是在视图中获取或更新数据到 vm.profileForm,而当你设置数据到视图表单控制器时,你正在向 vm 添加属性,而不是向 vm.profileForm

在$http成功改如下代码 来自

vm.DOB = res.result[0].DOB; vm.Country = res.result[0].Country; vm.State = res.result[0].State; vm.City = res.result[0].City; vm.postalCode = res.result[0].PostCode;

vm.profileForm.dob = res.result[0].DOB; vm.profileForm.Country = res.result[0].Country; vm.profileForm.State = res.result[0].State; vm.profileForm.City = res.result[0].City; vm.profileForm.postalCode = res.result[0].PostCode;

【讨论】:

  • 哈哈,是的,我只在控制器中向 vm 添加了属性,但是我应该在哪里查看,因为 ng-model 已经具有价值。
  • 您必须在控制器中设置 vm.DOB 的 vm.profileForm.dob。因为您在视图中使用了 vm.profileForm.dob。您必须为 Country、State、City 设置类似的方式
  • 现在我在控制器中有来自数据库的值,现在我如何发送值来查看?
猜你喜欢
  • 2021-11-03
  • 2021-12-27
  • 2022-01-09
  • 1970-01-01
  • 2014-10-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多