我在更新数据中使用时,ng-model值无效

时间:2017-02-17 08:50:31

标签: javascript angularjs angularjs-scope angularjs-ng-model

当我在我的控制器中使用此代码时。我认为ng-model可以根据输入权输出值?

  

ng-model =" $ scope.title"

但是当我在我的代码中实现更新数据时。我感到困惑,因为ng-model可以输出值,但即使我编辑了标题输入,该值仍会输出data.title。

我在" tes1234"中编辑了这个表格。但输出仍然是" tes"

有人给我解决方案吗?感谢。

已更新

这是我的HTML代码:

<div class="form-group">
<label class="control-label col-md-3">Title</label>
<div class="col-md-6">
<input type="text" name="title" class="form-control" ng-model="title">
</div>
</div>

$ scope.result从API获取数据然后我使用ng-repeat

HttpService("POST", url, param, function(response){

            $scope.parsing = angular.fromJson(response.data);

            $scope.result = {};

            angular.forEach($scope.parsing, function(item){

                $scope.result[item._id] =  item;


            });
        });

这是GetData()根据点击的数据获取数据并传入我的表单

<tbody ng-repeat="data in result">
                        <tr>
                            <td>
                                {{$index + 1}}
                            </td>
                            <td>
                                {{ data._id }}
                            </td>
                            <td>
                                {{ data.title }}
                            </td>
                            <td>
                                {{ data.category.label }}
                            </td>
                            <td>
                                {{ data.user.name }}
                            </td>
                            <td width="20%">
                                <button type="button" class="btn btn-primary" ng-click="getData(data)"><i class="fa fa-edit"></i> Edit</button>
                                <button type="button" class="btn btn-danger"><i class="fa fa-trash"></i> Delete</button>
                            </td>
                        </tr>
                    </tbody>

这是GetData()根据点击的数据获取数据并传入我的表单

$scope.getData = function(data) {

    $scope.title = data.title;

}

这是我更新数据后的保存

$scope.Save = function() {
    var data = $.param({
           title : $scope.title,
    });

    console.log(data);
};

基于API的数据对象

{
    "status": "200",
    "data": [
        {
            "_id": "589c0484a6551f948e1d6914",
            "parent_id": 0,
            "parent_source": 0,
            "category_id": "58942caba6551fd2c3347371",
            "user_id": "58942d43a6551fd7123bdcb1",
            "active": 1,
            "status": 1,
            "title": "coba tes",
            "description": "coba tes",
            "url": "coba-tes_6llapm",
            "extra": "EXTRA",
            "responded": "2017-02-09 12:56:20",
            "level": 0,
            "editor_pick": 0,
            "up_vote": 0,
            "down_vote": 0,
            "revision": 0,
            "answer_count": 2,
            "updated_at": "2017-02-09 13:04:14",
            "created_at": "2017-02-09 12:56:20",
            "tags": [],
            "user": {
                "_id": "58942d43a6551fd7123bdcb1",
                "status": 1,
                "username": "asdasdad",
                "email": "asdasdasd@gmail.com",
                "image": "https://scontent.xx.fbcdn.net/v/t1.0-1/p50x50/16299070_1114043338706757_6701359761657365227_n.jpg?oh=7ed22de2d576dc9d3cfd6a89aa386153&oe=5942BC1F",
                "about": "ini saya, saya suka makan dan belanja",
                "ref_id": "https://www.facebook.com/app_scoped_user_id/1104332756344482/",
                "name": "asdasd",
                "login_ip": "192.168.100.4",
                "notif_check": "2017-02-03 14:12:03",
                "token": "$2y$10$EMGp1wWnnPUDRJ/dSybCIeei88jROcAqsAsgXri2l8j/H8FMSt5iS",
                "updated_at": "2017-02-10 10:52:33",
                "created_at": "2017-02-03 14:12:03"
            },
            "category": {
                "_id": "58942caba6551fd2c3347371",
                "label": "My Kids and I",
                "active": 1,
                "url": "my-kids-and-i",
                "parent_id": 0,
                "level": 0,
                "dfp_interest": "[]",
                "meta_title": "",
                "meta_description": "",
                "meta_keyword": "",
                "updated_at": "2017-02-03 14:09:31",
                "created_at": "2017-02-03 14:09:31"
            }
        }]
}

3 个答案:

答案 0 :(得分:0)

只需将var data = $.param...更改为$scope.data=$.param...

答案 1 :(得分:0)

我添加了演示。

&#13;
&#13;
 <span class="e" ng-show="submitted && form.dob.$invalid">Please provide a valid date of birth</span>
&#13;
var app = angular.module('myApp', []);
app.controller('customersCtrl', function($scope, $http) {

  $scope.result = {
    "status": "200",
    "data": [{
      "_id": "589c0484a6551f948e1d6914",
      "parent_id": 0,
      "title": "coba tes",
    }]
  }



  $scope.getData = function(data) {
    console.log(data);
    $scope.title = data.title;

  };
  $scope.save = function() {
    console.log($scope.title);
    $scope.title;

  }

});
&#13;
&#13;
&#13;

答案 2 :(得分:0)

像这样更改getDate函数

$scope.getData = function(data) {
    $scope.title = data[0].title;
 }