[英]Submit object with ng-option
我在向控制器提交帶有ng-option的對象時遇到問題。 如果我提交表單,則Category
對象未定義。
有人可以指出我正確的方向嗎?
ItemsController
// Create new Item
$scope.create = function() {
// Create new Item object
var item = new Items ({
name: this.name,
...
category: this.category,
});
創建項目視圖
<section data-ng-controller="ItemsController">
<div class="page-header">
<h1>New Item</h1>
</div>
<div class="col-md-12">
<form class="form-horizontal" data-ng-submit="create()" novalidate>
<fieldset>
<div class="form-group">
<label class="control-label" for="name">Name</label>
<div class="controls">
<input type="text" data-ng-model="name" id="name" class="form-control" placeholder="Name" required>
</div>
</div>
...
</div>
<div class="form-group">
<label class="control-label" for="category">Category</label>
<div class="controls" data-ng-controller="CategoriesController" data-ng-init="find()">
<select data-ng-model="category" ng-options="cat.name for cat in categories"></select>
</div>
</div>
<div class="form-group">
<input type="submit" class="btn btn-default">
</div>
感謝“ aurav gupta”,我找到了解決方案http://fdietz.github.io/recipes-with-angular-js/controllers/sharing-code-between-controllers-using-services.html
問題是因為您使用的是兩個不同的控制器
因此它們將具有不同的范圍。
因此, 類別對象是為CategoriesController定義的,而不是ItemsController的
您可以通過將CategoriesController的代碼放在ItemsController中以制成單個控制器來解決此問題,也可以使用角度服務為兩個控制器綁定范圍變量。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.