简体   繁体   English

无法使用angular.js动态更新选择框

[英]Cant get Select Box to update dynamically with angular.js

I am currently learning angularJS and so far so good, but I'm having a problem that I cant seem to figure out. 我目前正在学习angularJS,到目前为止一直很好,但我遇到了一个我似乎无法弄清楚的问题。

I have 2 select boxes, one with top level categories in it, and a second one for sub-categories which is supposed to dynamically update when a change is made to the first select. 我有2个选择框,其中一个具有顶级类别,第二个用于子类别,当第一个选择发生更改时,它应该动态更新。

The first select box is populating fine, but despite the values being set for the second binding array by the event function I developed, the view never updates the second select box with the new values. 第一个选择框正在填充,但是尽管我开发的事件函数为第二个绑定数组设置了值,但视图永远不会使用新值更新第二个选择框。 I'm not sure what I'm doing wrong, and have tried a bunch of things but with no luck. 我不确定我做错了什么,并尝试了一些但没有运气的东西。

Here is what I've got: 这是我得到的:

var themebucketApp = angular.module('themebucketApp',[]);

//category list controller
themebucketApp.controller('CategoriesOptions',function($scope) {

    $scope.thememap = {"wordpress":[{"category":"Activism"},{"category":"Art"},{"category":"Blog \/ Magazine"},{"category":"BuddyPress"},{"category":"Business"},{"category":"Cart66"},{"category":"Charity"},{"category":"Children"},{"category":"Churches"},{"category":"Computer"},{"category":"Corporate"},{"category":"Creative"},{"category":"eCommerce"},{"category":"Entertainment"},{"category":"Environmental"},{"category":"Events"},{"category":"Experimental"},{"category":"Fashion"},{"category":"Film & TV"},{"category":"Food"},{"category":"Government"},{"category":"Health & Beauty"},{"category":"Hosting"},{"category":"Jigoshop"},{"category":"Marketing"},{"category":"Miscellaneous"},{"category":"Mobile"},{"category":"Music and Bands"},{"category":"News \/ Editorial"},{"category":"Nightlife"},{"category":"Nonprofit"},{"category":"Personal"},{"category":"Photography"},{"category":"Political"},{"category":"Portfolio"},{"category":"Restaurants & Cafes"},{"category":"Retail"},{"category":"Shopping"},{"category":"Software"},{"category":"Technology"},{"category":"Travel"},{"category":"Wedding"},{"category":"WooCommerce"},{"category":"WordPress"},{"category":"WP e-Commerce"}],"magento":[{"category":"Entertainment"},{"category":"Fashion"},{"category":"Health & Beauty"},{"category":"Magento"},{"category":"Miscellaneous"},{"category":"Shopping"},{"category":"Technology"}],"designs":[{"category":"404 Pages"},{"category":"Activism"},{"category":"Admin Templates"},{"category":"Art"},{"category":"Business"},{"category":"Charity"},{"category":"Children"},{"category":"Churches"},{"category":"Computer"},{"category":"Corporate"},{"category":"Creative"},{"category":"Electronics"},{"category":"Entertainment"},{"category":"Environmental"},{"category":"Events"},{"category":"Experimental"},{"category":"Fashion"},{"category":"Film & TV"},{"category":"Food"},{"category":"Government"},{"category":"Health & Beauty"},{"category":"Hosting"},{"category":"Marketing"},{"category":"Miscellaneous"},{"category":"Mobile"},{"category":"Music and Bands"},{"category":"Nightlife"},{"category":"Nonprofit"},{"category":"Personal"},{"category":"Photo Gallery"},{"category":"Photography"},{"category":"Political"},{"category":"Portfolio"},{"category":"Restaurants & Cafes"},{"category":"Resume \/ CV"},{"category":"Retail"},{"category":"Shopping"},{"category":"Site Templates"},{"category":"Social Media Home"},{"category":"Software"},{"category":"Specialty Pages"},{"category":"Technology"},{"category":"Travel"},{"category":"Under Construction"},{"category":"Virtual Business Card"},{"category":"Wedding"}],"opencart":[{"category":"Entertainment"},{"category":"Fashion"},{"category":"Miscellaneous"},{"category":"OpenCart"},{"category":"Shopping"},{"category":"Technology"}]};

    $scope.categories  = [{'name' : 'WordPress', 'value' : 'wordpress'},{'name' : 'Magento', 'value' : 'magento'},{'name' : 'OpenCart', 'value' : 'opencart'},{'name' : 'Designs', 'value' : 'designs'}];

    $scope.subcategories = [];

    $scope.loadSubcategories = function(){
        $scope.subcategories = $scope.thememap[$('#theme-type').val()];
    };

});

... ...

<div class="navbar-text pull-right" style="margin-top:10px;">
    <select id="theme-type" name="themetype" ng-controller="CategoriesOptions" ng-click="loadSubcategories();">
        <option value="">Theme Type</option>
        <option ng-repeat='category in categories' value="{{category.value}}">{{category.name}}</option>
    </select>
    <select id="theme-cat" name="themecat" ng-controller='CategoriesOptions'>
        <option value="All">All</option>
        <option ng-repeat='subcategory in subcategories' value='{{subcategory.category}}'>{{subcategory.category}}</option>
    </select>
</div>

Can anyone show me what I'm doing wrong or what I'm missing? 任何人都可以告诉我我做错了什么或我错过了什么?

There are several issues: 有几个问题:

One : ng-controller="CategoriesOptions" should be declared only once and not one per control Like this: ng-controller="CategoriesOptions"应该只声明一次而不是每个控件一个像这样:

<div class="navbar-text pull-right" style="margin-top:10px;" ng-controller="CategoriesOptions">
<select id="theme-type" name="themetype" ng-click="loadSubcategories();">
    <option value="">Theme Type</option>
    <option ng-repeat='category in categories' value="{{category.value}}">{{category.name}}</option>
</select>
<select id="theme-cat" name="themecat" >
    <option value="All">All</option>
    <option ng-repeat='subcategory in subcategories' value='{{subcategory.category}}'>{{subcategory.category}}</option>
</select>

Two for selects it better to use ng-options and ng-model : http://docs.angularjs.org/api/ng.directive:select 两个用于选择使用ng-optionsng-model更好: http//docs.angularjs.org/api/ng.directive :select

About: 关于:

ng-model="{string}"

It is what associates the selected value of the control with your model. 它将控件的选定值与您的模型相关联。

About: 关于:

 [ng-options="{comprehension_expression}"]

it works something like this: 它的工作原理如下:

<select ng-model="mySelectedValue" ng-options="c.name for c in categories"></select>

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

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