使用angularjs从后端填充表单

时间:2016-05-03 11:45:35

标签: javascript angularjs forms web-services angularjs-ng-model

我是angularjs的新手,我希望在用户想要更新他的个人资料时从数据库中提交一份表格。我有一个web服务,可以为已登录的用户提供所有信息,然后另一个webservice,以获取我需要的信息和用户的ID。这是我的控制器:

controller('myTalentisCtrl', function ($scope,$http) {
    this.User_Talent={};
    this.T_User={} ;

        $http.get("/LoggedUser").success(function(data) {
            alert(data);
            this.T_User.lnId = data.lnId;
            alert(this.T_User.lnId);

            });


        $http.get("/usertalent",this.T_User.lnId).success(function(data) {
            alert(data);
            this.User_Talent.user = data.user;
            this.User_Talent.talent = data.talent;
            });
        $scope.modif=function(){        

            $http.put('updating/' + this.T_User.lnId, $scope.User_Talent).success(function(data) {

                $scope.User_Talent = data;
                });

            };

这是我表格的一部分:

<div  ng-controller="myTalentisCtrl" class="tab-content no-margin">

    <!-- Tabs Content -->
    <div class="tab-pane with-bg active" id="fwv-1">

        <div class="row">

            <div class="col-md-4">
                <div class="form-group">
                    <label class="control-label" for="full_name">First Name</label>
                    <input type="text" ng-model="User_Talent.talent.strFirstName" class="form-control" name="first_name" id="first_name" data-validate="required" placeholder="Your first name" />
                </div>
            </div>
            <div class="col-md-8">
                <div class="form-group">
                    <label class="control-label" for="address_line_2">Address</label>
                    <input ng-model="User_Talent.talent.strAdress" class="form-control" name="address_line_2" id="address_line_2" placeholder="(Optional) your Address" />
                </div>
            </div>

        </div>
        <div class="row">

            <div class="col-md-6">
                <div class="form-group">
                    <label class="control-label" for="full_name">Last Name</label>
                    <input type="text" ng-model="User_Talent.talent.strLastName" class="form-control" name="last_name" id="last_name" data-validate="required" placeholder="Your last name" />
                </div>
            </div>

            <div class="col-md-6">
                <div class="form-group">
                    <label class="control-label" for="city">City</label>
                    <input ng-model="User_Talent.talent.strCity" class="form-control" name="city" id="city" data-validate="required" placeholder="Current city" />
                </div>
            </div>

        </div>
        <div class="row">

            <div class="col-md-6">
                <div class="form-group">
                    <label class="control-label" for="door_no">Phone Number</label>
                    <input ng-model="User_Talent.talent.lnPhone" class="form-control" name="phone_no" id="phone_no" data-validate="number" placeholder="Numbers only" />
                </div>
            </div>

            <div class="col-md-6">
                <div class="form-group">
                    <label class="control-label" for="zip">Zip</label>
                    <input ng-model="User_Talent.talent.lnZipCode" class="form-control" name="zip_no" id="zip_no" data-validate="number" placeholder="Numbers only" />
                </div>
            </div>

        </div>
        <div class="row">
            <div class="col-md-6">
                <div class="form-group">
                <label class="control-label" for="email">Email Adress</label>
                <div class="input-group">
                            <span class="input-group-addon">
                                <i class="linecons-mail"></i>
                            </span>
                            <input ng-model="User_Talent.talent.strEmail" type="email" class="form-control" placeholder="Enter your email">
                        </div>
                    <!-- <label class="control-label" for="city">Email Adress</label>
                    <input ng-model="User_Talent.talent.strEmail" class="form-control" name="city" id="city" data-validate="required" placeholder="Current city" /> -->
                </div>
            </div>

有没有办法用现有值初始化我的ng模型?

1 个答案:

答案 0 :(得分:0)

您应该执行摘要周期以反映DOM中的更改。

查看摘要 https://docs.angularjs.org/api/ng/type/ $ rootScope.Scope