gpt4 book ai didi

javascript - Angularjs 表单是只读的。为什么?

转载 作者:行者123 更新时间:2023-11-29 22:10:47 24 4
gpt4 key购买 nike

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

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

从rest中获取一个人并设置模板的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>

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

最佳答案

$http.get 不会向您的模型返回数据,因为它是异步的。它返回 Promise 对象。您需要在 .success() 回调中将结果值分配给 $scope.person:

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

请参阅documentation $http 示例

关于javascript - Angularjs 表单是只读的。为什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18152805/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com