【问题标题】:I want to set ng model's value with value attribute's value我想用值属性的值设置 ng 模型的值
【发布时间】:2017-07-10 09:24:13
【问题描述】:
<input type="text" value="{{id}}" class="form-control" id="pid" ng-model="user.id" readonly />

如果文本框的值为 1,我想将 user.id 值设置为 1

【问题讨论】:

  • 将您的 ng-model 更改为 id
  • 为什么要这样做?如果 id 是范围变量,那么在控制器中,您可以通过 $scope.user.id = $scope.id 对其进行初始化,然后,一旦您在文本框中进行更改,Angular 的双向数据绑定模型就会更新

标签: html angularjs angular-ngmodel


【解决方案1】:

您可以使用 Angular 的指令添加动态属性绑定。

<div ng-app="myApp" ng-controller="ctrl">
    <input type="text" ng-attr-value="{{title}}" class="form-control" id="pid" ng-model="title" readonly />
</div>

JS

var app = angular.module('myApp', []);
function ctrl($scope){
    $scope.title = "I 'm a tooltip!";    
}

Fiddle

【讨论】:

    【解决方案2】:

    试试下面的代码,

    var app = angular.module("app", []);
    
    app.controller("InputController", function($scope) {
        $scope.user={"id":1};
    });
    <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
    
    <body ng-app="app" ng-controller="InputController">
          <input type="text" value="{{user.id}}" class="form-control" id="pid" ng-model="user.id" readonly />
        </body>

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-01-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-03-12
      • 1970-01-01
      • 2023-03-22
      • 1970-01-01
      相关资源
      最近更新 更多