简体   繁体   English

加载页面时如何使用角复选框选中?

[英]While loading page how to make check box is checked using angular?

I have list of checkbox .based on checkbox select offers is coming everything is working fine.i have added my code below 我有复选框列表。基于复选框选择报价即将到来一切正常。我在下面添加了我的代码

 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> 

here when i open page nothing is checked in that checkbox list.my expectation when i come to this page it should check already selected value is there or not if it's there selected value should checked in that checkbox list. 在这里,当我打开页面时,该复选框列表中未检查任何内容。我期望当我进入此页面时,它应该检查是否已选择的值是否存在,是否应该在该复选框列表中检查所选择的值。 for example selected value Samsung Galaxy S6.in that checkbox list it should be checked .how to make this work pls some one help me out .i have tried but its not working for me i have modified my code but it's not working 例如选择值三星银河S6。在该复选框列表中应选中。如何使这项工作请一些人帮助我。我尝试过但不适用于我,我修改了我的代码但不起作用

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

here i have pushed selected value true but it's not working 在这里我已将选择的值推为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 here Samsung Galaxy S6 is checked means dropdown should come bcoz Samsung Galaxy S6 has offer 演示这里三星Galaxy S6被选中意味着下拉列表应该来bcoz三星Galaxy S6提供

Just use the ng-checked directive like this: 只需使用ng-checked指令,如下所示:

ng-checked="item.selected"

You should not combine ng-model and ng-checked. 您不应将ng-model和ng-checked结合使用。 From the docs: 从文档:

Note that this directive should not be used together with ngModel, as this can lead to unexpected behavior. 请注意,此指令不应与ngModel一起使用,因为这可能导致意外行为。

Of course you can also add the selected item to the selections array instead of having an additional selected flag on each item. 当然,您也可以将所选项目添加到selections数组中,而不是在每个项目上都具有附加的selected标志。 I would prefer this solution because it eliminate unnecessary redundancy. 我更喜欢这种解决方案,因为它消除了不必要的冗余。

Make sure you set selections to [] a the beginning and add each selected item: 确保将selections设置为[]开头,并添加每个选中的项目:

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

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

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