Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/395.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angularjs/25.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 AngularJs-forEach作用域数组-不工作_Javascript_Angularjs - Fatal编程技术网

Javascript AngularJs-forEach作用域数组-不工作

Javascript AngularJs-forEach作用域数组-不工作,javascript,angularjs,Javascript,Angularjs,我知道我在做傻事,所以请原谅我: $scope.entry = { Description: '' }; $scope.Type = 'Front'; $scope.entry.type = ''; $scope.$watch('Type', function (Type) { $scope.filterTypes = []; $scope.axleTypes = new API.GetAxleTypes(function (axleTypes) { a

我知道我在做傻事,所以请原谅我:

$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);
    });
});
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>
Html:

$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);
    });
});
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.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);
    });
});
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',函数(Type){
$scope.filterTypes=[];
如果($scope.axleTypes==未定义| |$scope.axleTypes==[])){
$scope.axleTypes=API.GetAxleTypes;
}
角度.forEach($scope.axleTypes,函数(类型){
log('axleType:'+type);
console.log('Type:'+Type);
if(axleType.Type==Type){
此.push(axleType);
}
},$scope.filterTypes);
$scope.filterTypes.sort(函数(a,b){
返回a.Description.localeCompare(b.Description);
});
});
我甚至不能在我的watch函数中循环通过axleTypes数组。它似乎没有拾取奇数集合,因为如果未定义或[],它会绕过填充axleTypes

我做了一件蠢事,我看不见

更新:根据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);
    });
});
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>
(2)我的Html:

$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);
    });
});
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>
过滤器:后部
正面

选择:

没有更多的错误,但是,当我切换两个单选按钮时,什么也没有发生,即,当我选择后轴单选按钮时,前轴仍然显示。啊。

好的,很酷。我想这就是你想要做的。基本上,您首先加载数据,然后设置手表。这样我们就不会多次调用异步调用。我修改了Plunker以使用此方法:


$scope.axleTypes=[]
永远不会为真<代码>[]=[]是
假的
。但是,还有未定义的,这是一个“或”的情况。你能举个例子吗?似乎有一种方法可以通过angular的内部过滤器来简化它。有人已经做了一个:问题是,一旦我使用API而不使用硬编码数组,我什么也得不到。我知道我的API是可以工作的,因为在这个指令上面我有:我猜你在使用$http.get()获取数据?在这种情况下,请检查您是否知道承诺-get()的结果不是object/array,而是一个将根据请求解析的承诺。Jason:查看我的新更新。我听从了你的建议,错误消失了,select中填充的初始轴是正确的。但当我选择后轴单选按钮时,$watch功能不起作用。请参见上面的编辑。我想您可能只是在回调中缺少了$digest循环;不起作用,真的不明白我为什么需要它,数据不是在plkr中硬编码的。[好的,这里有一个区别。这个示例没有涉及异步调用。这就是为什么我说您可能需要执行$scope。$digest()因为它不会在异步调用返回后自动运行。但是有一个更好的方法来处理这个问题。只需调用一次,而不是每次更改。我将编辑上面的示例。如果您看到“Chloe”,那是个错误……我女儿想看看她的名字在代码中是什么样子的。:)我现在回到工作版本。