1

我有一个控制器引入 json 文件的内容:

function phoneListController($scope, $http) {
 $http.get('phones.json').success(function(data) {
    $scope.phones = data.offers_basic_data;
    $scope.features = data.equipmentSearchFeatures;
 }).error(function(data, status, headers, config) {
    console.log('error');
 });
}

这很好用,但是我需要使用手机范围中的一项(产品 ID)来获取第二个范围中的正确功能。我是否放置了类似的东西:

var productid = phones.1.productid;

函数内?在页面上?我正在使用“ng-data-repeat”列出电话,我会以某种方式将变量放在重复中吗?我尝试了多种方法将产品 ID {{phone.1.ProductID}} 插入功能范围:{{features.productID.0}} 但我在那里做的任何事情似乎都不起作用。有什么想法/建议吗?

如果我使用了不正确的术语,请纠正我,我使用 angularJS 不到一周!


我试图在函数中分配一个变量,但它不起作用:

var productid = phones[1]['productID'];

这就是我在 HTML 中的调用方式:

Test: {{features.productid.0}}


好的,我真的很接近,我知道我错过了一些非常愚蠢/很少的东西:

$scope.phones = data.offers_basic_data;
console.log(JSON.stringify(phones));
var productid = $scope.phones[1]['ProductID'];
$scope.features = data.equipmentSearchFeatures['Features'][productid];

以上不起作用,但手机再次在页面上列出,而不是白色。我觉得我没有将变量正确地合并到数组中。


这是第二个数组的截图:

[equipmentSearchFeatures] => Array
    (
        [Features] => Array
            (
                [12312] => Array
                    (
                        [0] => Test - no

接下来尝试使用此行,仍然显示电话,但不显示测试功能:

$scope.features = data.equipmentSearchFeatures['Features'][$scope.phones[1]['ProductID']];
4

3 回答 3

0

根据您的编辑,并假设属性名称是 ProductID:

var productid = $scope.phones[1].ProductID;

编辑

好的,我想我明白了——data.equipmentSearchFeatures有一个名为 Features 的属性,它是一个数组,并且该数组的成员有一个名为 productId 的属性,您想找到与您的变量 productId 匹配的属性吗?

为此,您将需要一个循环。这没有检查语法,但应该很接近:

var productid = $scope.phones[1].ProductID; //get the product id

//loop over the Features collection until you match the productId
angular.forEach(data.equipmentSearchFeatures.Features, function(feature){
  if (feature.productId == productId){
    //now set the scope variable to the matching item
    $scope.features = feature;
  }
};
于 2013-10-29T12:54:16.673 回答
0

@cfox:试试这个:

console.log(JSON.stringify(phones));

看看你是否在寻找合适的元素!

但我想你想做

var productid = $scope.phones[1].ProductID;

反而!

于 2013-10-29T13:11:13.060 回答
0

通过重新格式化显着重新格式化将功能直接移动到产品下而不是单独的数组的 json 文件来解决。我确信有一种方法可以使用 AngularJS 来解决这个问题,但是不到一周的时间我就无法弄清楚那个解决方案。

于 2013-10-29T18:11:29.303 回答