ui-select:从控制器设置值

时间:2017-04-06 10:00:47

标签: angularjs ui-select

我有两个ui-select:第一个允许设置食物的类别(例如肉类,蔬菜,甜点);第二种允许从食物清单中挑选食物。 插入新食物时,会填充所有字段,包括类别(通过ui-select)。这是HTML:

<ui-select ng-model="newFood.category" theme="bootstrap">
     <ui-select-match placeholder="Select food category">
           <span ng-bind="$select.selected.name"></span>
     </ui-select-match>
     <ui-select-choices repeat="category in (foodCategories | filter: $select.search)">
           <span ng-bind="category.name"></span>
     </ui-select-choices>
</ui-select>

当想要编辑现有食物时,他们使用第二个ui-select来挑选食物,我希望第一个选择获得其类别值。这是HTML:

<ui-select ng-model="foodToEditOrRemove.selected" theme="bootstrap" on-select="fillFormFields()">
   <ui-select-match placeholder="Select food to edit or remove">
         <span ng-bind="$select.selected.name"></span>
   </ui-select-match>
   <ui-select-choices repeat="food in (foods | filter: $select.search)">
         <span ng-bind="food.name"></span>
   </ui-select-choices>
</ui-select>

在我的控制器中,从ui-select中选择一个类别后,$scope.newFood包含我输入的正确值,包括类别。从其他ui-select中挑选食物后,调用fillFormFields:

$scope.fillFormFields = function () {
       $scope.newFood.name = $scope.foodToEditOrRemove.selected.name;
        $scope.newFood.category = $scope.foodToEditOrRemove.selected.category;

    };

在此代码之后,newFood具有正确的类别集,我在相应的输入中看到食物名称,但是ui-select不显示该值。如何更新视图以绑定到新值?

0 个答案:

没有答案