简体   繁体   English

从控制器填写值时,角度选项数组变得不确定

[英]Angular option array getting undefined when filling in values from controller

I used this answer: 我用这个答案:

Working with select using AngularJS's ng-options 使用AngularJS的ng-options选择

To try and get a dropdown list of options to fill from my AddCtrl controller: 要尝试从我的AddCtrl控制器获取要填充的选项的下拉列表,请AddCtrl

$scope.newMeal = {};

        $scope.newMeal = {
            name: "",
            price: "",
            ingredients: "",
            description: "",
            category: "",
            cuisine: ""
        };

        $scope.categories = [
            { id: 1, name: 'Breakfast' },
            { id: 2, name: 'Lunch' },
            { id: 3, name: 'Dinner' }
        ];

        $scope.cuisines = [
            { id: 1, name: 'Thai' },
            { id: 2, name: 'Chinese' },
            { id: 3, name: 'Italian' },
            { id: 4, name: 'British' },
            { id: 5, name: 'Spanish' },
            { id: 6, name: 'Indian' },
            { id: 7, name: 'French' },
            { id: 8, name: 'Vietnamese' },
            { id: 9, name: 'Nordic' }
        ];

Into my form: 放入我的表格:

<div class="list list-inset">
                    <label class="item item-input item-select">
                        <div class="input-label">
                            Category
                        </div>
                        <select ng-model="newMeal.category" ng-options="category as categories.name for category in categories">
                            <option value="" ng-if="newMeal.category">&nbsp;</option>
                        </select>
                    </label>
                    <label class="item item-input item-select">
                        <div class="input-label">
                            Cuisine
                        </div>
                        <select ng-model="newMeal.cuisine" ng-options="cuisine as cuisines.name for cuisine in cuisines">
                            <option value="" ng-if="newMeal.cuisine">&nbsp;</option>
                        </select>
                    </label>
                </div>

However I'm getting the number of values to be filled in to be appearing, they all appear as "undefined" in the option dropdowns. 但是,我要填写的值数量已经出现,它们在选项下拉列表中都显示为“ undefined”。 How can I fix this? 我怎样才能解决这个问题? Note I want there to be a value of nothing if nothing is selected, and this default none value to disappear when anything is clicked. 注意,如果没有选择任何内容,我希望没有值,并且单击任何东西时此默认值都不消失。

Actually, you made just a little mistake: 实际上,您犯了一个小错误:

Where you wrote the expression inside the ngOptions: 在ngOptions中写表达式的位置:

category as categories.name for category in categories

cuisine as cuisines.name for cuisine in cuisines

You could have use just: 您可以只使用:

category.name for category in categories

cuisine.name for cuisine in cuisines

Remember that, by default, when you don't have the "as" inside the expression, the entire object is assined to the $scope variable defined inside the ngModel. 请记住,默认情况下,当表达式中没有“ as”时,整个对象将被关联到ngModel内定义的$ scope变量。 In this way, you don't need to declare category as category.name and cuisine as cuisine.name. 这样,您无需将category声明为category.name,而将菜色声明为cuisine.name。

If you want to bind just the name of the category and cuisine, you also could use: 如果您只想绑定类别和美食的名称,则还可以使用:

category.name as category.name for category in categories
cuisine.name as cuisine.name for cuisine in cuisines

Another important thing is that the ngModel already initializes the values and in this way you don't need to declare inside the controller: 另一个重要的事情是ngModel已经初始化了值,因此您无需在控制器内部声明:

$scope.newMeal = {}; // In this case this is useless because you are defining this property again in the next line.
$scope.newMeal = {
  name: "",
  price: "",
  ingredients: "",
  description: "",
  category: "",
  cuisine: ""
};

Let ngModel initializes it for you if you need it. 如果需要,请让ngModel为其初始化。

Following, the final code: 以下是最终代码:

Controller 控制者

$scope.categories = [
    { id: 1, name: 'Breakfast' },
    { id: 2, name: 'Lunch' },
    { id: 3, name: 'Dinner' }
];

$scope.cuisines = [
    { id: 1, name: 'Thai' },
    { id: 2, name: 'Chinese' },
    { id: 3, name: 'Italian' },
    { id: 4, name: 'British' },
    { id: 5, name: 'Spanish' },
    { id: 6, name: 'Indian' },
    { id: 7, name: 'French' },
    { id: 8, name: 'Vietnamese' },
    { id: 9, name: 'Nordic' }
];

View 视图

<div class="list list-inset">
    <label class="item item-input item-select">
        <div class="input-label">
            Category
        </div>
        <select ng-model="newMeal.category" ng-options="category.name for category in categories">
            <option value="" ng-if="newMeal.category">&nbsp;</option>
        </select>
    </label>
    <label class="item item-input item-select">
        <div class="input-label">
            Cuisine
        </div>
        <select ng-model="newMeal.cuisine" ng-options="cuisine.name for cuisine in cuisines">
            <option value="" ng-if="newMeal.cuisine">&nbsp;</option>
        </select>
    </label>
</div>

The problem is that you're overriding the variable for 'category for' with the variable 'category as' in the select statement by using 'category' for both places. 问题是您要在select语句中使用两个地方的“ category”来覆盖变量“ category for”和“ category as”。 Ie

category as category.name for category in categories

Use the below code to make it work 使用以下代码使其正常工作

<select ng-model="newMeal.category" ng-options="category as cat.name for cat in categories">
      <option value="" ng-if="newMeal.category">&nbsp;</option>
</select>

and for cuisines this one: 对于美食而言:

<select ng-model="newMeal.cuisine" ng-options="cuisine as cuis.name for cuis in cuisines">
        <option value="" ng-if="newMeal.cuisine">&nbsp;</option>
</select>

Here's a working JSBIN 这是一个工作的JSBIN

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM