2017-07-06 40 views
0

我有一个对象数组,显示在我的作用域中,一旦通过两个下拉选择框选择,第一个框选择第二个选择格式的名称。一个对象的从角度范围获取ID值并用于陈述

例如

{ 
    "name": "tim", 
      "format": [ 
       {"Fname": "a", "id": "1"}, 
       {"Fname": "b", "id": "2"}, 
       {"Fname": "c", "id": "3"} 
      ] 
     }, 

,我将其发送到使用

$scope.productTypeChange = function() { 
     $scope.formats = $scope.productsandformats.find(ps => ps.name == $scope.formData.ProductType.name) 
} 

我得到一品脱了我的范围,想

{"ProductType":{"name":"tim"},"formatType":{"Fname":"a","id":"1"}} 

那么我想有范围一个if语句基于在范围中打印的ID值,我已经尝试过以获得ID值,但它看起来似乎不起作用,Is这是获取ID的正确方法>

$scope.setCanvasSize = function() { 
    divHeight = $('.image-builder').height(); 
    if ($scope.formData.ProductType.name.id = 1) { 
     // Aratio = 0.67; 
     Aratio = 2; 
    } else if ($scope.formData.ProductType.name.id = 2) { 
     Aratio = 0.56; 
    } else if ($scope.formData.ProductType.name.id = 3) { 
     divHeight = divHeight/1.5; 
     Aratio = 2; 
    } else if ($scope.formData.ProductType.name.id = 4) { 
     Aratio = 0.67; 
    } else { 
     Aratio = 1 
    } 
    canvas.setHeight(divHeight - 15); 
    canvas.setWidth((divHeight - 15) * Aratio); 
    canvas.renderAll(); 
    $scope.position(); 
}; 
+0

问题是什么? – Vivz

+0

Il为你和其他人更新问题 – Beep

+0

你想获得ID吗? – Vivz

回答

0

您正在错误地使用该对象。如果

$scope.formats={"ProductType":{"name":"tim"},"formatType":{"Fname":"a","id":"1"}}; 

然后拿到ID,你必须访问对象这样

$scope.formats.formatType.id 

您也可以在if语句正确地检查。在你的问题你分配值而不是比较。因此,它应该是这样的:

if($scope.formats.formatType.id=="1"){ 
//do calculations 
} 
+0

所以我的If语句将是如果'($ scope.formats.formatType.id = 1){ somthing}'? – Beep

+0

它应该是如果($ scope.formats.formatType.id ==“1”),因为id是字符串,从你的问题 – Vivz

+0

可以看出完美,谢谢你的帮助 – Beep

0

你为什么要访问“值”使用数组(假设,因为你使用find),如“$ scope.formData.ProductType.name.id”而不是使用“$ scope.formats.formatType.id”(您已经过滤)访问值。

此外,在过滤变量中,您没有匹配“$ scope.formData.ProductType.name.id”类型的格式,而是必须像这样访问变量 - >“$ scope。 formats.formatType.id“