1. 次のマークアップを持つ AxleTypes というコレクションを持つ Truck クラスがあります。
public class AxleType : Entity
{
public string Description { get; set; }
public string Type { get; set; }
}
2.私のAngularフォームには次のものが含まれています(これをできるだけ短くするために、フォームの他のアクスルタイプを省略しました。これはテンプレートであり、メインフォームに含まれているため、$parentを使用しています):
<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)
];
デバッグすると、これが最終結果です。
これをできるだけ短くするために、上記の 2 番目の車軸タイプの選択については説明しませんでした。しかし、ご覧のとおり、サーバーは 2 つの車軸タイプを取得していますが、各 [タイプと説明] の両方のプロパティが null です。誰か提案はありますか?
console.log では、両方の値が「未定義」です。
興味深い観察:
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-model にあると考えるようになります。ただし、サーバー クラスの AxleType ではなく、Description などのプロパティが 1 つしかない場合は、次の方法で選択の説明を追加するだけです。
newTruck.AxleTypes = [
angular.copy($scope.selectedFrontAxle),
angular.copy($scope.selectedRearAxle)
];
値が渡されました。非常に紛らわしいです。