Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/71.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 repeat get current clicked item$scope中的Angular指令_Javascript_Jquery_Angularjs - Fatal编程技术网

Javascript ng repeat get current clicked item$scope中的Angular指令

Javascript ng repeat get current clicked item$scope中的Angular指令,javascript,jquery,angularjs,Javascript,Jquery,Angularjs,我创建了一个指令,其中包含一个日期选择器,称为calendar。实际的日期选择器javascript已经在JQuery中创建,为了能够设置ng模型,我找到了以下代码: var clickedID = "#" + $scope.indexid + "_datePicker"; $(clickedID).trigger('input'); 问题是$scope.indexid始终显示为日历指令数组中的最后一个值 例如,为了澄清,指令将我的变量显示如下: [ { indexid: 1}, {

我创建了一个指令,其中包含一个日期选择器,称为calendar。实际的日期选择器javascript已经在JQuery中创建,为了能够设置ng模型,我找到了以下代码:

var clickedID = "#" + $scope.indexid + "_datePicker";
$(clickedID).trigger('input');
问题是$scope.indexid始终显示为日历指令数组中的最后一个值

例如,为了澄清,指令将我的变量显示如下:

[
  { indexid: 1},
  { indexid: 2},
  { indexid: 3}
]
当在页面$scope.indexid中单击任何日历时,该日历将始终返回3(数组中的最后一项)。我如何解决这个问题

这是一个很难解释的问题。如果你想了解更多信息,请告诉我。我会尽量按要求增加

编辑更多代码:

//Index Page

<div ng-repeat="item in items">

 <calendar indexid="{{$index}}"></calendar>

</div>


//In the Directive
scope: {
  ngModel: "=",
  indexid: "@"
}

//In Directive Controller
console.log($scope.indexid);   

//In the directive template
<input type="text" ng-model="testModel" id="{{::indexid}}"/>
//索引页
//在指令中
范围:{
ngModel:“=”,
indexid:“@”
}
//指令内控制器
log($scope.indexid);
//在指令模板中

在数组上循环时,需要通过引用传入每个对象才能在其上设置值。你这样做:

<div ng-repeat="item in items">
  <calendar item="item"></calendar>
</div>
scope: {
  item: "=" // reference to 'item' from the parent scope ... your loop
},
template: '<input type="text" ng-model="testModel" id="{{::item.indexid}}"/>',
link: function ($scope) {
  console.log($scope.item.indexid);

  console.log($scope.testModel); // this is the value of the input
}

此模式应使您能够更好地访问每个数组项。通过引用传递它,你可以写回它;指令内部对
项所做的更改将显示在外部作用域上的
数组中。

当您在数组上循环时,您需要通过引用传入每个对象才能在其上设置值。你这样做:

<div ng-repeat="item in items">
  <calendar item="item"></calendar>
</div>
scope: {
  item: "=" // reference to 'item' from the parent scope ... your loop
},
template: '<input type="text" ng-model="testModel" id="{{::item.indexid}}"/>',
link: function ($scope) {
  console.log($scope.item.indexid);

  console.log($scope.testModel); // this is the value of the input
}

此模式应使您能够更好地访问每个数组项。通过引用传递它,你可以写回它;指令内部对
item
所做的更改将显示在外部作用域上的
items
数组中。

这似乎是一个对javascript本身的作用域和循环理解不好的问题。你能提供一段更大的代码吗?@DeblatonJean-Philippe当然我会尽可能多地添加。我同意@DeblatonJean-Philippe,此外,在不触发$digest循环的情况下更改数据可能会出现问题。@AshleyCoolman似乎我无法获取this.calendItem,它会引用上次创建的项目。嗯,当您将
id=“{{::indexid}}
更改为
id=“{indexid}}时会发生什么
?这似乎是一个对javascript本身的作用域和循环理解不好的问题。你能提供一段更大的代码吗?@DeblatonJean-Philippe当然我会尽可能多地添加。我同意@DeblatonJean-Philippe,此外,在不触发$digest循环的情况下更改数据可能会出现问题。@AshleyCoolman似乎我无法获取this.calendItem,它会引用上次创建的项目。嗯,当您将
id=“{{::indexid}}
更改为
id=“{indexid}
时会发生什么?