2013-03-14 21 views
0

1.我有了一個集合稱爲與以下標記AxleTypes一個卡車類:AngularJS加入2個屬性相關聯的集合

public class AxleType : Entity 
{ 
    public string Description { get; set; } 
    public string Type { get; set; } 
} 

2.我的角形式包括以下(以保持此儘可能短我省略形式的其他橋型,和我使用的$父,因爲這是一個模板,並在主要形式通NG-包括):

<label for="frontAxles">Front Axle: </label> 
<input ng-model="$parent.frontAxleType" type="hidden" value="Front"> 
<select ng-model="$parent.selectedFrontAxle" ng-options="axleType.Description for axleType in axleTypes"> 
    <option value="" selected>Select Axle Type ..</option> 
</select> 

3.主要形式插入通過卡車控制器新的卡車,因此在卡車控制器:

a. I instantiate an instance of the AxleTypes collection so the form is populates the select w/axle types. 
    b. I instantiate an instance of AxleType to pass the selected data from the form. 
    c. I pass the respective ng-models on the form to the AxleType variable. 
    d. I add that AxleType variable to the Truck's AxleTypes collection. 

    a: if ($scope.axleTypes == undefined || !($scope.axleTypes.length > 0)) 
     { $scope.axleTypes = API.GetAxleTypes(); } 

    b: $scope.axleType = {}; 

    c: var frontAxle = $scope.axleType; 
     frontAxle.Description = $scope.selectedFrontAxle; 
     frontAxle.Type = $scope.frontAxleType; 

    d: newTruck.AxleTypes = [ 
     angular.copy(frontAxle) 
    ]; 

當調試這是最終結果是:

enter image description here

爲了保持這儘可能短我沒有說明第二軸類型選擇上面。但是,正如您所看到的,服務器正在拾取2個軸類型,但是每個[類型&描述]的屬性均爲空。 有沒有人有任何建議?

在console.log中,這兩個值都是「undefined」。

一個有趣的現象:

當我硬編碼在TruckCtrl價值觀,一切工作正常:

frontAxle.Description = 'Some Front Value'; 
    frontAxle.Type = 'Front'; 
    rearAxle.Description = 'Some Rear Value'; 
    rearAxle.Type = 'Rear'; 

    newTruck.AxleTypes = [ 
     angular.copy(frontAxle), 
     angular.copy(rearAxle) 
    ]; 

這將導致有人認爲問題出在NG-模型在軸模板上。然而,當我只有一個屬性,就是說明,而不是鍵入服務器類,AxleType,並通過只是增加了選擇的描述:

newTruck.AxleTypes = [ 
     angular.copy($scope.selectedFrontAxle), 
     angular.copy($scope.selectedRearAxle) 
    ]; 

傳遞的價值觀。很混亂。

+1

這與angularjs有什麼關係?從我看到的調試截圖是從C#(?)? – 2013-03-14 18:56:28

+0

我沒有看到,對不起。好的,你如何將數據發送到服務器?你在使用$資源服務嗎?或$ http? – 2013-03-14 19:03:10

回答

1

已解決!

我很想告訴40位一些奇怪的眼球,他們在沒有輸入的情況下仔細研究了這個問題,而Stewie,我們知道Stewie是什麼。但我不會。相反,我向那些有同樣問題的人提供幫助。

好的,就解決了。

問題#1:Angular不接受html輸入中的默認值。

我很困惑,爲什麼我有像這樣的輸入:

<input ng-model="$parent.selectedRearType" type="hidden" value="Front"> 

,但角度說,它沒有價值。

問題的解決方案#1:

需要初始化在你的控制器以下幾點:

$scope.selectedFrontType = 'Front'; 
    $scope.selectedRearType = 'Rear'; 

問題2:你是如何傳遞多個屬性值的集合嗎?

儘管這種情況有多現實世界,但令人不安的是有關於此事的ZERO文檔。

問題的解決方案#2:

在我的HTML我有這個select語句:

<select ng-model="$parent.selectedFrontAxle" ng-options="axleType.Description for axleType in axleTypes"> 

哪裏犯了錯誤就在想這個NG-模型是嚴格的Description屬性類AxleType(見上面我的類模型)。 這是一個巨大的錯誤,事實並非如此。 ng-model不是該類的Description屬性,而是實際上是整個類本身。因此,在檢查select的ng模型時,即使只提供了Description屬性,也可以將它完整地表示爲AxleType類。那麼,什麼NG-模型,給我的是這是在我的控制器返回值的用戶作出的選擇之後:

AxleType類=>描述=「無論選擇的人」,類型=「」

在這種情況下,我需要填寫角度沒有的空白,即在這種情況下,Type屬性。

這裏的整體解決方案:

// beginning of controller when it initializes 
    $scope.selectedFrontType = 'Front'; 
    $scope.selectedRearType = 'Rear';  

    // $scope.axleType = {}; -> NOT NEEDED 

    // in the save method  
    // axles 
    var frontAxle = $scope.selectedFrontAxle; 
    frontAxle.Type = $scope.selectedFrontType; 

    var rearAxle = $scope.selectedRearAxle; 
    rearAxle.Type = $scope.selectedRearType; 

    newTruck.AxleTypes = [ 
     angular.copy(frontAxle), 
     angular.copy(rearAxle) 
    ]; 

enter image description here

我希望我幫助別人!