2

我如何在 html 中定义我的应用程序:

<div ng-app="md-app">
    <div id="general">
        <ng-include src="template1" ng-controller="GeneralCtrl"></ng-include>
    </div>
</div>

从休息中获取人并设置模板的js:

function GeneralCtrl($scope, $http, $location) {
    $scope.template1 = 'some_path';

    $scope.person = $http.get('route', {id: personId})).then(function(response){
        return response.data;
    }); 
}

在模板中,我可以读取所有数据。我有一个表单来编辑这个人的一些数据,但是默认情况下该表单是只读的:

 <form>
     <div class="form-group">
         <input type="text" class="form-control" ng-model="person.nick"/>
     </div>
 </form>

该人的昵称显示在输入字段中,但我无法对其进行编辑。当我开始输入时,它会被忽略。为什么?

4

1 回答 1

2

$http.get doesn't return data to your model, as it is asynchronous. It returns the Promise object. You need to assign the resulting value to $scope.person in the .success() callback:

$scope.person = {};
$http.get('route', {id: personId})).success(function(response){
    $scope.person = response.data;
});

Please see the documentation for $http with examples

于 2013-08-09T18:09:40.227 回答