【发布时间】:2014-01-05 03:24:21
【问题描述】:
如何使用 angular 正确地将对象(嵌套)添加(推送)到 json。
查看现场工作演示:JSbin Link
我有一个具有特定结构的机场工厂:
angApp.factory("Airports", function () {
var Airports = {};
Airports.detail = {
"PDX": {
"code": "PDX",
"name": "Portland International Airport",
"city": "Portland"
},
"STL": {
"code": "STL",
"name": "Lambert-St. Louis International Airport",
"city": "St. Louis"
},
"MCI": {
"code": "MCI",
"name": "Kansas City International Airport",
"city": "Kansas City"
}
};
return Airports;
});
与控制器链接: 我如何编写一个正确的方法来将输入推送到 Airport.detail?
.controller("AirportsCtrl", function ($scope, Airports) {
$scope.formURL = "views/_form.html";
$scope.currentAirport = null;
$scope.airports = Airports;
$scope.setAirport = function (code) {
$scope.currentAirport = $scope.airports.detail[code];
};
$scope.addAirport = function() {
$scope.airports.push();
};
});
HTML:
我应该在 ng-model 中添加什么来正确地将对象推送到 Airport.details
添加机场
身份证号:
<div class="form-group">
<label >code:</label><br>
<input class="form-control" type="text" placeholder="eg. PDX">
</div>
<div class="form-group">
<label>Name:</label><br>
<input class="form-control" type="text" ng-model="" placeholder="eg. Portland Intl. Airport">
</div>
<div class="form-group">
<label>City</label><br>
<input class="form-control"type="text" ng-model="" placeholder="eg. Portland">
</div>
<input class="btn btn-primary" type="submit">
</form>
【问题讨论】:
标签: javascript json angularjs angularjs-directive yeoman