javascript - 第一个 DropDown 选择不会触发第二个 DropDown 函数

标签 javascript jquery html angularjs drop-down-menu

我在 MVC 中有 2 个 DropDowns 并尝试使用 AngularJS。我的第一个 DropDown 填充了正确来自数据库的数据。当用户在第一个 DropDown 上进行选择时,第二个 DropDown 应根据选择进行填充。但是,它不会被触发。我做错了什么?

当我从第一个下拉列表中选择时,Chrome 控制台显示此错误“未定义”。但是,已经定义了,为什么会显示这个错误呢?

这是我的 html 代码...

<form name="mainForm" ng-controller="PriceListEditController" novalidate>
    <div class="error">{{message}}</div>
    <select id="brandSelect" ng-model="brandSelect" ng-options="b.Id as b.Name for b in brands" ng-change="GetBrandPriceList()">
        <option value="">Marka Seçiniz</option>
        <option ng-repeat="b.Id for b in brands" value="{{b.Id}}">{{b.Name}}</option>
    </select>
    <select ng-model="priceList" ng-options="b.Id as b.Name for b in priceLists">
        <option value="">-- Select Country --</option>
        <option ng-repeat="p.Id for p in priceLists" value="{{p.Id}}">{{p.Name}}</option>
    </select>
</form>

这是我的 JS 代码...

var app = angular.module('PriceListEditModule', []);
app.controller('PriceListEditController', ["$scope", "$q", "$http", function ($scope, $q, $http) {

    $http({
        method: 'GET',
        url: '/GetBrands'
    }).then(function successCallback(response) {
        $scope.brands = response.data;
    }, function errorCallback(response) {
        $scope.message = 'Unexpected Error ' + response.message;
    });

    $scope.GetBrandPriceList = function () {

        console.log($scope.brandSelect);

        var brandId = $scope.brandSelect;

        if (brandId>0) {
            $http({
                method: 'GET',
                url: '/GetBrandPriceList'
            }).then(function successCallback(response) {
                $scope.priceLists = response.data;
            }, function errorCallback(response) {
                $scope.message = 'Unexpected Error ' + response.message;
            });
        }
        else {
            $scope.priceList = null;
        }
    }

}]);    

最佳答案

您在同一选择部分中使用 ng-options 和 ng-repeat。 在你的 html 中试试这个:

<form name="mainForm" ng-controller="PriceListEditController" novalidate>
        <div class="error">{{message}}</div>
        <select id="brandSelect" ng-model="brandSelect" ng-change="GetBrandPriceList()">
            <option value="">Marka Seçiniz</option>
            <option ng-repeat="b in brands" value="{{b.Id}}">{{b.Name}}</option>
        </select>
        test
        <select ng-model="priceList">
            <option value="">-- Select Country --</option>
            <option ng-repeat="p in priceLists" value="{{p.Id}}">{{p.Name}}</option>
        </select>
    </form>

关于javascript - 第一个 DropDown 选择不会触发第二个 DropDown 函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41881551/

相关文章:

javascript - 通过我的 api 发送数据不起作用

jquery - 设置 jQuery 小部件 div 的样式

javascript - 如果 jquery 验证激活,如何在单选按钮中放置红色边框

javascript - JQuery-使用 Var 提交表单

html - 当其他 col-6 隐藏在小屏幕上时,让 col-6 占据整行

javascript - 当文本框字段为空时,如何将文本框边框颜色突出显示为红色?

javascript - AngularJS ng-repeat 和 json 数据的问题

javascript - 如何为适用于 html 5 浏览器和旧版浏览器的 type=date 输入设置默认值

javascript - CKEditor - insertText 在 setData 后不工作

html - 溢出时下拉菜单不起作用 :hidden