与模型的基本$ scope连接

时间:2016-12-30 01:50:20

标签: angularjs

我的代码:

    .controller("GetAllAuthors", function ($scope, $http) {
    $http.get('http://localhost:8080/authors')
        .then(function (response) {
            $scope.authors = response.data;
        });
    $scope.edit = function (index) {
        for (var i = 0; i < $scope.authors.length; i++) {
            if ($scope.authors[i].id == index) {
                $scope.object = $scope.authors[i];
                break;
            }
        }
    }
})

Html视图:

<tbody ng-repeat="author in authors">
<td><input type="button" ng-click="edit(author.id)" value="Edit"/></td>

<div ng-controller="GetAllAuthors">
    {{object.id}} // <--- doesn't display it
</div>

它不起作用。我无法使用日期绑定我的对象。如何解决?

2 个答案:

答案 0 :(得分:0)

你需要把http放在编辑方法中。

 .controller("GetAllAuthors", function ($scope, $http) {

    $scope.edit = function (index) {

         $http.get('http://localhost:8080/authors')
        .then(function (response) {
            $scope.authors = response.data;

            for (var i = 0; i < $scope.authors.length; i++) {
               if ($scope.authors[i].id == index) {
                   $scope.object = $scope.authors[i];
                   break;
               }
            }
        });


    }
})

答案 1 :(得分:0)

试试这个 在函数外部初始化$ scope.object。