【问题标题】:Unable to bind $scope objects in ng-model for input无法在 ng-model 中绑定 $scope 对象以进行输入
【发布时间】:2018-12-30 13:19:13
【问题描述】:

我正在尝试使用 ng-model 将我的 Controller 的对象值绑定到输入,如果我添加它 ng-repeat 它是绑定数据,但如果我直接调用它,它不会将数据绑定到输入。谢谢你:)

角度控制器

SMSApp.controller('studentUpdateController', function ($scope, $routeParams, GetStudentService) {
    $scope.stuid = $routeParams.STUDENTID != null ? $routeParams.STUDENTID : 0;
    GetStudentService.getbyId($scope.stuid).then(function (result) {
        $scope.obj = JSON.parse(result);
        $scope.obj = $scope.obj.Table;
        console.log($scope.obj);
    });
});

控制台日志

HTML 代码

<div class="col-md-4 form-group">
    <label>First Name</label>
    <input type="text" class="form-control" ng-model="obj.FIRSTNAME" placeholder="First Name" required />
</div>
<div class="col-md-4 form-group">
    <label>Middle Name</label>
    <input type="text" class="form-control" ng-model="obj.MIDDLENAME" value="" placeholder="Middle Name" required />
</div>

【问题讨论】:

  • 控制台输出显示它的一个数组。将$scope.obj.Table 更改为$scope.obj.Table[0]
  • 永远欢迎朋友。

标签: angularjs json dom data-binding angular-ngmodel


【解决方案1】:

这是因为你的 obj 是一个数组。如果您不想使用 ng-repeat 换句话说,如果您确定您的数组中只有 1 个项目,您可以尝试以下操作

<div class="col-md-4 form-group">
    <label>First Name</label>
    <input type="text" class="form-control" ng-model="obj[0].FIRSTNAME" placeholder="First Name" required />
</div>
<div class="col-md-4 form-group">
    <label>Middle Name</label>
    <input type="text" class="form-control" ng-model="obj[0].MIDDLENAME" value="" placeholder="Middle Name" required />
</div> 

【讨论】:

  • 很高兴能帮上忙。
猜你喜欢
  • 2021-01-19
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-04-26
  • 1970-01-01
  • 2016-11-18
相关资源
最近更新 更多