2013-03-14 49 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

我希望我帮助别人!