在获得NG-模型输入默认数据 [英] getting default data in ng-model input

查看:97
本文介绍了在获得NG-模型输入默认数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

HTML

 < D​​IV>
  <输入NG模型=tripsheet.tripsheet_num类型=文本>
< / DIV>

JS

  $ scope.getTotalTripsheets =功能()
 {
  返回$ scope.tripsheets.length;
 };

我想上面的数据(getTotalTripsheets)对默认的输入输入标签,在上面codeI正在获取从数据库表的长度,并试图使其作为输入字段的默认数据。

我尝试财产以后这样的(如下图),它的DOS不行,我应该如何着手呢?

  $ scope.tripsheet =功能()
 {
  $ scope.tripsheet_num = $ scope.getTotalTripsheets!;
 };


解决方案

如果你只是想一次性/单向绑定到一个的长度阵列,使用 NG-值而不是 NG-模型

<输入NG值=tripsheets.length类型=文本>

如果你有一个双向绑定,可以从默认的改变:

$ scope.tripsheet = {
  tripsheet_num:$ scope.tripsheets.length
};

<输入NG模型=tripsheet.tripsheet_num类型=文本>

实例plunker: http://plnkr.co/edit/UM4lyGZSxB1mEcpy9CqJ?p = preVIEW

最后,这听起来像你可能会做一个到数据库的远程调用,所以你可以使用以下策略:

$ scope.tripsheet = {
  $解决:假的,
  tripsheet_num:空
};

当远程调用成功,将 $解决真正并设置<$ C $的价值C> tripsheet_num 。在HTML,你可以做这样的事情:

&LT;!。$ tripsheet解决输入NG-禁用= NG-模式=tripsheet.tripsheet_num类型= 文本&GT;

在plunker解决例如: http://plnkr.co/edit/q2Ua8KGqWr6HDphe92q0? p = preVIEW

HTML

<div>
  <input ng-model="tripsheet.tripsheet_num" type="text"> 
</div>

JS

$scope.getTotalTripsheets = function() 
 {
  return $scope.tripsheets.length;
 };

i want the above data(getTotalTripsheets) to to default input in the input tag, in the above code i am fetching the length from the database table and tried making it as the default data in the input field.

i tried somthing like this(below), it dos not work, how should i proceed with this?

$scope.tripsheet = function()
 {
  $scope.tripsheet_num = !$scope.getTotalTripsheets;
 };

解决方案

If you just want a one-time/one-way bind to the length of an Array, use ng-value instead of ng-model:

<input ng-value="tripsheets.length" type="text">

If you have a two-way binding that can be changed from the default:

$scope.tripsheet = {
  tripsheet_num: $scope.tripsheets.length
};

<input ng-model="tripsheet.tripsheet_num" type="text">

Example in plunker: http://plnkr.co/edit/UM4lyGZSxB1mEcpy9CqJ?p=preview

Finally, it sounds like you may be doing a remote call to a database, so you could use the following strategy:

$scope.tripsheet = {
  $resolved: false,
  tripsheet_num: null
};

When the remote call succeeds, set $resolved to true and also set the value of tripsheet_num. In the HTML you could do something like this:

<input ng-disabled="!tripsheet.$resolved" ng-model="tripsheet.tripsheet_num" type="text">

Resolve example on plunker: http://plnkr.co/edit/q2Ua8KGqWr6HDphe92q0?p=preview

这篇关于在获得NG-模型输入默认数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆