AngularJS:Array.prototype.find()在Chrome中不起作用
问题描述:
我遇到了一个问题,我的角码在firefox中工作,但没有在Chrome中。AngularJS:Array.prototype.find()在Chrome中不起作用
浏览器控制台打印此:
TypeError: undefined is not a function
at setSelectedColor (http://localhost:3000/assets/products/controllers/mens_detail_controller.js?body=1:24:54)
这是我的控制器(注意console.log
s的在故障发生):
app.controller('MensDetailCtrl', ['$scope', '$resource', '$controller', 'Product', function($scope, $resource, $controller, Product) {
$controller('ProductsDetailCtrl', {$scope: $scope});
$scope.init = function(id, locale, selected_color_id)
{
$scope.product = Product.get({productId: id, locale: locale}, function(data) {
var unsorted_products_colors = data.products_colors.filter(function(product_color) {
return product_color.mens == true;
});
$scope.products_colors = unsorted_products_colors.sort(function(a, b) {
return a.mens_sort_order > b.mens_sort_order;
});
setSelectedColor(selected_color_id);
});
}
var setSelectedColor = function(selected_color_id) {
console.log("ffff"); //prints
if (selected_color_id) {
console.log("eeeffff"); //prints
// the error seems to happen at this next line, which is line 24:
$scope.selected_color = $scope.products_colors.find(function(el) {
return el.id == selected_color_id;
});
} else {
console.log("hhffff");
$scope.selected_color = $scope.products_colors[0];
}
console.log("ffffzzz"); // does NOT print
$scope.selected_variant = $scope.selected_color.variants[0];
$scope.sorted_product_images = $scope.selected_color.product_images.sort(function(a, b) {
return a.sort_order > b.sort_order;
});
$scope.selected_image = $scope.sorted_product_images[0];
}
}]);
为什么它的工作完全罚款的Firefox,但不是在Chrome上?我将如何解决这个问题?
== == UPDATE即使 当我在飞机坠毁前右打印出的$scope.products_colors
价值,它打印出一个对象在我的浏览器控制台:
ffff
eeeffff
[Object]0: Object$$hashKey: "004"color: Objectcolor_id: 32created_at: "2014-08-12T19:47:32.000Z"id: 91mens: truemens_sort_order: 0product_id: 15product_images: Array[2]updated_at: "2014-08-12T19:47:32.000Z"variants: Array[1]womens: truewomens_sort_order: 0__proto__: Objectlength: 1__proto__: Array[0]
==更新2 ==
卸下.find(...)
,并与数组下标替换它修复了错误,但它不修复该错误,因为我需要抓住有特定属性的元素:
var setSelectedColor = function(selected_color_id) {
if (selected_color_id) {
// $scope.selected_color = $scope.products_colors.find(function(el) {
// return el.id == selected_color_id;
// });
$scope.selected_color = $scope.products_colors[0]
} else {
$scope.selected_color = $scope.products_colors[0];
}
$scope.selected_variant = $scope.selected_color.variants[0];
$scope.sorted_product_images = $scope.selected_color.product_images.sort(function(a, b) {
return a.sort_order > b.sort_order;
});
$scope.selected_image = $scope.sorted_product_images[0];
}
更换
.find
可能会改变'VAR setSelectedColor =功能(selected_color_id){''到功能setSelectedColor(selected_color_id){'当你调用初始化?但是,这个代码是如何显示的? – PSL 2014-09-22 18:42:11Nope没有运气... Yeh这是我的确切的控制器代码(除了注释) – Edmund 2014-09-22 18:44:17
$ scopes.products_colors在崩溃前的价值是什么?错误指向第54列,在那里你调用'find',这样可以帮助你缩小范围... $ scopes.products_colors在firefox和chrome中都有find属性吗? – 2014-09-22 18:45:28