AngularJS文本输入使用ng模型,但需要默认值,而不使用value=或ng-init=

AngularJS text input with ng-model but needs default value not working with value= or ng-init=

本文关键字:ng-init value 默认值 输入 文本 ng 模型 AngularJS      更新时间:2023-09-26

我有一个包含许多输入的帐户窗体。其中一个如下:

<input class="form-control " placeholder="First Name" type="text" ng-model="account.first_name" ng-init="account.first_name=user.account.first_name">

我的控制器中有一个服务,可以返回用户的所有信息。我试图在输入中显示文件中的当前信息。

我希望这是一个输入,这样他们也可以改变(更新)它

然而,当我将API从服务返回的value="{{user.account.first_name}}"设置为等于$scope.user时,它在输入中不显示任何内容。

我在SO上搜索答案,发现了一些建议ng-init的答案,但仍然不起作用。

我还尝试了ng-bindng-value,并将它们放在设置ng-model之前。

我该怎么做?

编辑:

这是我的账户管理员。该服务只有return $http.get('/api/account');

app.controller('AccountCtrl', function($scope, Account) {
    Account.show()
        .success(function(data) {
            $scope.user = data;
        });

data是一个JSON模式,类似于:

{"account": {}, "documents": {}}

但里面有数据。

从服务获得数据后,在控制器中执行此操作

      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)
 });

你为什么不省心,把账户业务从模型中删除呢。我不知道它有什么功能,只是使用用户对象。双重选择:

在控制器中:$scope.account=angular.copy($scope.user);