Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angularjs/22.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 在ng模型输入中获取默认数据_Javascript_Angularjs - Fatal编程技术网

Javascript 在ng模型输入中获取默认数据

Javascript 在ng模型输入中获取默认数据,javascript,angularjs,Javascript,Angularjs,HTML 我希望上面的数据(getTotalTripsheets)成为输入标记中的默认输入,在上面的代码中,我从数据库表中获取长度,并尝试将其作为输入字段中的默认数据 我尝试过这样的事情(如下),它不起作用,我应该如何继续 $scope.getTotalTripsheets = function() { return $scope.tripsheets.length; }; 您正在输入一个tripsheet.tripsheet_num的模型,这意味着在$scope上将有一个名为tri

HTML

我希望上面的数据(getTotalTripsheets)成为输入标记中的默认输入,在上面的代码中,我从数据库表中获取长度,并尝试将其作为输入字段中的默认数据

我尝试过这样的事情(如下),它不起作用,我应该如何继续

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

您正在输入一个tripsheet.tripsheet_num的模型,这意味着在$scope上将有一个名为tripsheet.tripsheet_num的属性(我想,不确定,因为它实际上会做什么。)

但是我知道,当您将tripsheet num设置为函数时,它会覆盖以前的任何内容,并且您应该会丢失.tripshee_num属性。你的结构应该更像这样

$scope.tripsheet = function()
 {
  $scope.tripsheet_num = !$scope.getTotalTripsheets;
 };
如果tripsheet是一个数组,则将您输入的模型更改为“tripsheetLength”,并将代码更改为此

$scope.tripsheet = {}
$scope.tripsheet.tripsheet_num = "default value";

$scope.tripsheets=['info1','info2']
$scope.tripsheetLength=$scope.tripsheets.length;//这里的长度是2

如果您只想对
数组的长度进行一次性/单向绑定,请使用
ng value
而不是
ng model


如果您的双向绑定可以从默认值更改为:

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

plunker中的示例:

最后,听起来您可能正在对数据库进行远程调用,因此可以使用以下策略:

$scope.tripsheet={
$已解决:错误,
tripsheet_num:空
};
远程调用成功后,将
$resolved
设置为
true
,并设置
tripsheet_num
的值。在HTML中,您可以执行以下操作:



解析plunker上的示例:

您的问题确实不清楚。您将getTotalTripsheets定义为一个函数,然后将其用作属性
$scope.getTotalTripsheets
。此外,您的代码毫无意义$scope.getTotalTripsheets'这是我尝试过的,我真的不清楚。我尝试使用“ng value”在输入中获取“getTotalTripsheets”数据,但ng模型的存在会覆盖它,默认情况下,我应该如何将数据库的长度获取到输入标记中。我需要ng模型,因为我正在将ng模型=“tripsheet.tripsheet_num”传递到mysql数据库。不,这不是一次性的,也不是静态数据,我想得到length@user3689593我添加了一个使用默认值集的双向绑定的示例。嘿,谢谢,真的很有帮助。非常感谢,代码确实获得了默认值,但是我如何获得数据库的长度而不是“默认值”,只要将其设置为您想要的任何值即可。长度在哪里?tripsheets就像一张单行票据(Receipt)。数据存储在mysql中。下面的代码获取tripsheets的长度<代码>$scope.getTotalTripsheets=function(){return$scope.tripsheets.length;}该代码不起任何作用—您只需创建一个函数并返回变量的长度。您需要执行某种ajax调用来从服务器获取信息。我在段落标记中使用
{{{getTotalTripsheets()}}
来获取数据,但在默认值区域中使用它是行不通的。
$scope.tripsheet = {}
$scope.tripsheet.tripsheet_num = "default value";
<input ng-model="tripsheetLength" type="text"> 
$scope.tripsheets = ['info1', 'info2']
$scope.tripsheetLength = $scope.tripsheets.length; // length here is 2