AngularJs - forEach作用域数组-不工作
AngularJs - forEach scope array - not working
我知道我做了一些愚蠢的事情,所以请原谅我:
Html:Filter: <input type="radio" ng-model="Type" value="Rear"> Rear
<input type="radio" ng-model="Type" value="Front"> Front
<br>
Select:
<name-value-select entry="entry" field="axleType" options="filterTypes"></name-value-select>
控制器:
$scope.$watch('Type', function (Type) {
$scope.filterTypes = [];
if ($scope.axleTypes == undefined || $scope.axleTypes == []) {
$scope.axleTypes = API.GetAxleTypes;
}
angular.forEach($scope.axleTypes, function (type) {
console.log('axleType: ' + type);
console.log('Type: ' + type);
if (axleType.Type == Type) {
this.push(axleType);
}
}, $scope.filterTypes);
$scope.filterTypes.sort(function (a, b) {
return a.Description.localeCompare(b.Description);
});
});
我甚至不能在watch函数中循环遍历axleTypes数组。它似乎没有拾取一个奇怪的集合,因为它绕过填充未定义或[]的axleTypes。
我在做蠢事,我自己都看不出来。
Update: Per Jason's request
(1) My angular controller:
$scope.entry = {
Description: ''
};
$scope.Type = 'Front';
$scope.entry.type = '';
$scope.$watch('Type', function (Type) {
$scope.filterTypes = [];
$scope.axleTypes = new API.GetAxleTypes(function (axleTypes) {
angular.forEach($scope.axleTypes, function (axleType) {
if (axleType.Type == Type) {
this.push(axleType);
}
}, $scope.filterTypes);
});
$scope.filterTypes.sort(function (a, b) {
return a.Description.localeCompare(b.Description);
});
});
(2)我的Html:
Filter: <input type="radio" ng-model="Type" value="Rear"> Rear
<input type="radio" ng-model="Type" value="Front"> Front
<br>
Select:
<name-value-select entry="entry" field="axleType" options="filterTypes"></name-value-select>
没有更多的错误杰森,然而,当我切换两个单选按钮什么都没有发生,即,前轴仍然显示当我选择后轴单选按钮。啊。
很好。我觉得这就是你想做的。基本上,你要先加载数据,然后设置手表。这样我们就不会多次调用异步调用。我修改了普朗克使用这种方法http://plnkr.co/edit/lQbn4hXmJ1Z4YpKdb4u3?p=preview:
$scope.axleTypes = API.GetAxleTypes(function () {
$scope.$watch('Type', function (Type) {
$scope.filterTypes = [];
angular.forEach($scope.axleTypes, function (type) {
console.log('axleType: ' + type);
console.log('Type: ' + type);
if (axleType.Type == Type) {
this.push(axleType);
}
if(!$scope.$$phase) $scope.$digest();
}, $scope.filterTypes);
});
});
$scope.filterTypes.sort(function (a, b) {
return a.Description.localeCompare(b.Description);
});
相关文章:
- Var工作,但在使用数组执行相同操作时停止
- 2D数组在Angular JS 1.2.x中工作,而不是在1.3.x中工作
- 为什么AngularJS$scope.watch()在我告诉它监视数组时会停止工作
- 如何制作Javascript's减少对数组数组的方法工作
- 试着让数组工作
- 如何让Knockout可观察数组与jQuery对话框一起工作
- 当许多元素相等时,Javascript数组排序无法正常工作
- AngularStrap Select和ng选项don'使用动态数组不能很好地工作
- 从数组中随机选择,但在for循环中选择是不可能的;没有按预期工作
- map() 函数如何在数组上工作
- Javascript数组未按预期工作
- 如何在谷歌工作表中使用数组公式时使用getLastRow.我可以让它忽略一列吗
- Web工作程序在处理大型数组时内存不足
- Polymer v1.3.1数据绑定无论是否使用带有重复模板和json数组的iron ajax都无法工作
- Knockout.js多个可观察数组不工作
- 解析数组时 for 循环无法正常工作
- ngRepeat:dupes 使用名为“name”的数组在中继器中复制键.用其他名称,它是工作
- 谷歌地图API v3动态标记,帮助使数组工作
- js 数组排序无法正常工作
- JavaScript发送变量,但插入数组PHP不能正常工作