Javascript ng repeat get current clicked item$scope中的Angular指令
我创建了一个指令,其中包含一个日期选择器,称为calendar。实际的日期选择器javascript已经在JQuery中创建,为了能够设置ng模型,我找到了以下代码: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}, {
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}
时会发生什么?