我认识到我做了些蠢事,所以请放纵我:
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函数中的axisTypes数组循环。它似乎没有拾取一个奇怪的集合,因为如果未定义或[]它绕过填充轴类型。
我正在做一些如此愚蠢的事情,我看不到它。更新:根据Jason的要求
(1)我的角度控制器:
$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>
没有更多的错误Jason,然而,当我切换两个单选按钮没有任何反应时,即当我选择后轴单选按钮时前轴仍然显示。 Ughh。
答案 0 :(得分:1)
好的,很酷。我想这就是你想要做的。基本上,您首先加载数据,然后设置手表。这样我们就不会多次调用异步调用。我修改了Plunker以使用这种方法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);
});