[英]AngularJS text input with ng-model but needs default value not working with value= or ng-init=
I have an account form with many inputs. 我有一个有很多输入的帐户表格。 One of them below: 以下之一:
<input class="form-control " placeholder="First Name" type="text" ng-model="account.first_name" ng-init="account.first_name=user.account.first_name">
I have a service in my controller that returns all of the user's information. 我的控制器中有一个服务,可以返回所有用户信息。 I'm trying to, in inputs, show the current information on file. 我试图在输入中显示文件中的当前信息。
I want this in an input so they can also change (update) it. 我希望在输入中提供此信息,以便他们也可以更改(更新)它。
However, when I set value="{{user.account.first_name}}"
which is returned by my API from my service and set equal to $scope.user
, it shows nothing in the input. 但是,当我设置由我的API从服务返回的value="{{user.account.first_name}}"
并设置为$scope.user
,它在输入中什么也不显示。
I searched for answers on SO and found a few that suggested ng-init
, which still didn't work. 我在SO上搜索了答案,并发现了一些建议ng-init
,但仍然没有用。
I've also tried ng-bind
, ng-value
, and putting these before I set ng-model
. 我还尝试了ng-bind
, ng-value
,并将它们放在我设置ng-model
。
How can I do this? 我怎样才能做到这一点?
Edit: 编辑:
Here is my account controller. 这是我的客户经理。 The service just has return $http.get('/api/account');
该服务仅return $http.get('/api/account');
app.controller('AccountCtrl', function($scope, Account) {
Account.show()
.success(function(data) {
$scope.user = data;
});
data
is a JSON schema like so: data
是一个JSON模式,如下所示:
{"account": {}, "documents": {}}
But with data inside. 但是里面有数据。
Do that in controller after you get data from your service 从服务中获取数据后,在控制器中执行此操作
Account.show()
.success(function (data) {
$scope.user = data;
//set account.first_name here
$scope.account = {
first_name: $scope.user.account.first_name
};
//or you can use angular.copy(source, destination)
angular.copy(data.account, $scope.account)
});
Why don't you save yourself the headache and drop the account business off the model. 您为什么不省却自己的麻烦,而将帐户业务从模型中删除。 I don't see what function it serves, just use the user object. 我看不到它提供的功能,只是使用用户对象。 Double alternative: 双重选择:
In your controller: $scope.account = angular.copy($scope.user); 在您的控制器中:$ scope.account = angular.copy($ scope.user);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.