2017-07-06 39 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「