设置selectedIndex属性后,data-ng-change不起作用

时间:2017-08-14 10:24:35

标签: javascript angularjs

var app = angular.module('app', []);
        app.controller('appController', ['$scope', function($scope) {
            $scope.allTheme = [{
                "theme_id": 2,
                "productData": [{
                    "store_id": 1,
                    "product_id": 3,
                    "style_id": 1,
                    "size_id": 3,
                    "theme_id": 2,
                    "name": "Boy - FD",
                }, {
                    "store_id": 1,
                    "product_id": 4,
                    "style_id": 1,
                    "size_id": 3,
                    "theme_id": 2,
                    "name": "Boy - FE"
                }, {
                    "store_id": 1,
                    "product_id": 8,
                    "style_id": 1,
                    "size_id": 3,
                    "theme_id": 2,
                    "name": "Boy - QS",
                }]
            }, {
                "theme_id": 5,
                "productData": [{
                    "store_id": 1,
                    "product_id": 99,
                    "style_id": 1,
                    "size_id": 3,
                    "theme_id": 5,
                    "name": "Blank - FD"
                }, {
                    "store_id": 1,
                    "product_id": 100,
                    "style_id": 1,
                    "size_id": 3,
                    "theme_id": 5,
                    "name": "Blank - FE"
                }, {
                    "store_id": 1,
                    "product_id": 101,
                    "style_id": 1,
                    "size_id": 3,
                    "theme_id": 5,
                    "name": "Blank - QS"
                }]
            }, {
                "theme_id": 7,
                "productData": [{
                    "store_id": 1,
                    "product_id": 129,
                    "style_id": 1,
                    "size_id": 3,
                    "theme_id": 7,
                    "name": "Nautical"
                }]
            }, {
                "theme_id": 10,
                "productData": [{
                    "store_id": 1,
                    "product_id": 12,
                    "style_id": 1,
                    "size_id": 3,
                    "theme_id": 10,
                    "name": "Girl - FD"
                }, {
                    "store_id": 1,
                    "product_id": 13,
                    "style_id": 1,
                    "size_id": 3,
                    "theme_id": 10,
                    "name": "Girl - FE"
                }, {
                    "store_id": 1,
                    "product_id": 17,
                    "style_id": 1,
                    "size_id": 3,
                    "theme_id": 10,
                    "name": "Girl - QS"
                }]
            }];

            $scope.targetField = null;
            $scope.selectBoxClick = function($event) {
                if ($event.target === null) {
                    return;
                }
                $scope.targetField = $event.target;
            }

            $scope.changeTheme = function(theme, selectedProducts) {
                console.log("chagne Theme");
                if ($scope.targetField) {
                    $scope.activeTheme = theme;
                    if (selectedProducts) {
                        $scope.isSizedAndThemeSelected = true;
                        var targetSelect_ = $($scope.targetField);
                        $scope.targetField = null;
                        targetSelect_.closest(".owl-item").siblings().each(function() {
                            if ($(this).find("select").length) {
                                var option = $(this).find("select option").eq(0);
                                //$(this).find("select").selectbox("change", option.attr('value'), option.html());
                                var select_ = $(this).find("select");
                                select_.val(option.attr('value'));
                                select_.prop('selectedIndex',0);
                                
                            }
                        })
                    } else {
                        $scope.isSizedAndThemeSelected = false;
                        $scope.activeTheme = {};
                    }
                }
            }

        }]);
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<script src="http://project-progress.net/projects/kodak-express-local-angular/js/jquery.selectbox-0.2.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.6.4/angular.min.js"></script>

<div data-ng-app="app">
  <div data-ng-controller="appController">
    <div data-ng-repeat="theme in allTheme" style="width:500px;" class="owl-item">
      <div class="select-box" data-ng-if="theme.productData.length > 1">
        <!--<select class="customSelect" data-ng-model="theme.SelectedProduct" data-ng-click="selectBoxClick($event)" data-ng-change="changeTheme(theme, theme.SelectedProduct)">
                <option value="">Select an option</option>
                <option data-custom-select-item data-ng-repeat="product in theme.productData" data-ng-value="{{product}}">{{product.name}}</option>
            </select>-->
        <select data-ng-model="theme.SelectedProduct" data-ng-click="selectBoxClick($event)" data-ng-change="changeTheme(theme, theme.SelectedProduct)">
                <option value="">Select an option</option>
                <option data-ng-repeat="product in theme.productData" data-ng-value="{{product}}">{{product.name}}</option>
            </select>
      </div>
    </div>
  </div>
</div>

如果我从第一个选择框选项中选择'Boy - FD'并且在第二个选择框选项之后,选择'boy-fed'两个都可以起作用,但是再次选择第一个选择框'Boy-FD'选项比功能不起作用。提前谢谢。

1 个答案:

答案 0 :(得分:1)

您的下拉菜单无法正常工作的原因是,当您直接更新DOM时,您的数据模型会与&#34; data-ng-model&#34;不要更新,因为你更新了角度消化周期之外的下拉列表。

以下是我如何让您的下拉列表与角度一起工作,我希望这会有所帮助。

&#13;
&#13;
var app = angular.module('app', []);
app.controller('appController', ['$scope', function($scope) {
  $scope.allTheme = [{
    "theme_id": 2,
    "productData": [{
      "store_id": 1,
      "product_id": 3,
      "style_id": 1,
      "size_id": 3,
      "theme_id": 2,
      "name": "Boy - FD",
    }, {
      "store_id": 1,
      "product_id": 4,
      "style_id": 1,
      "size_id": 3,
      "theme_id": 2,
      "name": "Boy - FE"
    }, {
      "store_id": 1,
      "product_id": 8,
      "style_id": 1,
      "size_id": 3,
      "theme_id": 2,
      "name": "Boy - QS",
    }]
  }, {
    "theme_id": 5,
    "productData": [{
      "store_id": 1,
      "product_id": 99,
      "style_id": 1,
      "size_id": 3,
      "theme_id": 5,
      "name": "Blank - FD"
    }, {
      "store_id": 1,
      "product_id": 100,
      "style_id": 1,
      "size_id": 3,
      "theme_id": 5,
      "name": "Blank - FE"
    }, {
      "store_id": 1,
      "product_id": 101,
      "style_id": 1,
      "size_id": 3,
      "theme_id": 5,
      "name": "Blank - QS"
    }]
  }, {
    "theme_id": 7,
    "productData": [{
      "store_id": 1,
      "product_id": 129,
      "style_id": 1,
      "size_id": 3,
      "theme_id": 7,
      "name": "Nautical"
    }]
  }, {
    "theme_id": 10,
    "productData": [{
      "store_id": 1,
      "product_id": 12,
      "style_id": 1,
      "size_id": 3,
      "theme_id": 10,
      "name": "Girl - FD"
    }, {
      "store_id": 1,
      "product_id": 13,
      "style_id": 1,
      "size_id": 3,
      "theme_id": 10,
      "name": "Girl - FE"
    }, {
      "store_id": 1,
      "product_id": 17,
      "style_id": 1,
      "size_id": 3,
      "theme_id": 10,
      "name": "Girl - QS"
    }]
  }];

  $scope.changeTheme = function(theme) {
    console.log("change Theme");
    $scope.activeTheme = theme;
    angular.forEach($scope.allTheme, function(item, index) {
      if (item.SelectedProduct != theme.SelectedProduct) {
        item.SelectedProduct = "";
      }
    });
    console.log(theme.SelectedProduct);

  }

}]);
&#13;
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<script src="http://project-progress.net/projects/kodak-express-local-angular/js/jquery.selectbox-0.2.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.6.4/angular.min.js"></script>

<div data-ng-app="app">
  <div data-ng-controller="appController">
    <div data-ng-repeat="theme in allTheme" style="width:500px;" class="owl-item">
      <div class="select-box" data-ng-if="theme.productData.length > 1">
        <select data-ng-model="theme.SelectedProduct" data-ng-change="changeTheme(theme)">
                <option value="">Select an option</option>
                <option data-ng-repeat="product in theme.productData" data-ng-value="{{product}}">{{product.name}}</option>
            </select>
      </div>
    </div>
  </div>
</div>
&#13;
&#13;
&#13;