javascript - 加载页面时如何使用 Angular 检查复选框?

标签 javascript jquery html angularjs checkbox

我有复选框列表。基于复选框选择优惠即将到来,一切正常。我在下面添加了我的代码

function Test1Controller($scope) {
  
    var serverData = ["Samsung Galaxy Note", "Samsung Galaxy S6", "Samsung Galaxy Avant","Samsung Galaxy Young"];
    $scope.items= [] ;

    for(var i=0;i<serverData.length;i++)
    {
        var modal = {
        name:serverData[i],
        selected:false
        };  
		
        $scope.items.push(modal);        
    }
    //----------------------------Our Shop Offers----------------------------------------
    $scope.offers = [
    {
        id: "as23456",
        Store: "samsung",
        Offer_message:"1500rs off",
        modalname: "Samsung Galaxy Young"       

    },{
        id: "de34575",
        Store: "samsung",
        Offer_message:"20% Flat on Samsung Galaxy S6",
        modalname: "Samsung Galaxy S6"       

    },
    ]
    //-----------------------------------------------------------------------------------
	$scope.selection = [];

      $scope.toggleSelection = function toggleSelection(item) {
     $scope.gotOffers=[];
      var idx = $scope.selection.indexOf(item);

      // is currently selected
      if (idx > -1) {
        $scope.selection.splice(idx, 1);
      }

      // is newly selected
      else {
        $scope.selection.push(item);
      }

        for(var i=0;i<$scope.selection.length;i++){
          for(var j=0;j<$scope.offers.length;j++){
            console.log($scope.selection[i].name  +"   "+ $scope.offers[j].modalname)
            if( $scope.selection[i].name  == $scope.offers[j].modalname){
              var idx = $scope.gotOffers.indexOf($scope.offers[j].Offer_message);
              if(idx == -1){
                console.log("inside idx")
                $scope.gotOffers.push($scope.offers[j]);
              }

            }
          }

        }
		console.log($scope.offers);
		
    };
	//---------------------------------------------------------------------------------------
     $scope.check = function()
	 
     {
	 
	 
         var checkedItems = [];
         console.log($scope.offerSelected)
            for(var i=0;i<$scope.items.length;i++)
            {
                  if($scope.items[i].selected){
                      checkedItems.push($scope.items[i].name);
                    }
            }
              console.log(checkedItems) ; 
     }
		


}
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.1.5/angular.min.js"></script>

<div ng-app>
  <div ng-controller="Test1Controller" >
    <div ng-repeat="item in items">
      <input type="checkbox" ng-model="item.selected"  ng-checked="selection.indexOf(item) > -1" ng-click="toggleSelection(item)"/> {{item.name}}
    </div>
    <select ng-show="gotOffers.length > 0" ng-model="offerSelected">
      <option ng-repeat="offer in gotOffers"  value="{{offer.id}}">{{offer.Offer_message}}</option>
    </select>

      <input type="button" name="submit" value="submit" ng-click="check()"/>
  </div>
</div>

这里,当我打开页面时,该复选框列表中没有任何内容被选中。我的期望是,当我进入此页面时,它应该检查已选择的值是否存在,如果存在,则应在该复选框列表中检查所选值。 例如,选择值三星 Galaxy S6。在该复选框列表中应该选中它。如何进行这项工作,请有人帮助我。我已经尝试过,但它不适合我 我已经修改了代码,但它不起作用

//var selectedvalue = window.localStorage.getItem("selectedvalue");
    // here selected value Samsung Galaxy S6
    var selectedvalue="Samsung Galaxy S6";

这里我已将所选值推为 true,但它不起作用

for(var i=0;i<serverData.length;i++)
    {
        var modal = {
        name:serverData[i],
        selected:false
        };  
        if (selectedvalue.indexOf(serverData[i]) >= 0 || null)
        {
                        modal.selected = true;

        }
        $scope.items.push(modal);        
    }

demo 此处选中 Samsung Galaxy S6 意味着应该出现下拉菜单,因为 Samsung Galaxy S6 有优惠

最佳答案

只需使用 ng-checked 指令,如下所示:

ng-checked="item.selected"

您不应该将 ng-model 和 ng-checked 结合起来。来自文档:

Note that this directive should not be used together with ngModel, as this can lead to unexpected behavior.

当然,您也可以将所选项目添加到 selections 数组中,而不是在每个项目上添加额外的 selected 标志。我更喜欢这个解决方案,因为它消除了不必要的冗余。

确保将 selections 设置为 [] 开头并添加每个选定的项目:

if (selectedvalue.indexOf(serverData[i]) >= 0 || null) {
    selection.push(modal);
}

关于javascript - 加载页面时如何使用 Angular 检查复选框?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34225864/

相关文章:

html - 使用微格式将谷歌搜索结果样式化为 stackoverflow

javascript - 如何定位 Internet Explorer 脚本错误?

jquery - html隐藏字段的更改事件

jquery - 使用 Backbone.js 的数据表/网格小部件

javascript - 我可以使用哪些方法来重新排列 javascript 函数的执行顺序?

jquery - 悬停时逐行突出显示段落

xml - 仅在 IE10 中出现“XML5619 : Incorrect document syntax.”错误

javascript - 使用外部 react 组件缺少 react

javascript - Jquery 如果 keydown 和 keyup 事件触发禁用其他按键事件,则在 Chrome 中有效,但在 IE 和 Mozilla 中无效

javascript - 在tinyMCE的上下文菜单中添加 "text color"