我有复选框列表。基于复选框选择优惠即将推出一切正常。我已添加下面的代码
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>
这里当我打开页面时没有检查该复选框list.my期望当我来到这个页面它应该检查已经选择的值是否存在,如果它的选择值应该在该复选框列表中检查。 例如选择值三星Galaxy S6.in复选框列表应该检查。如何使这项工作请一些人帮助我。我试过但它不适合我 我修改了我的代码,但它无法正常工作
//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 这里三星Galaxy S6被检查意味着下降应该来bcoz三星Galaxy S6提供
答案 0 :(得分:1)
只需使用ng-checked
指令,如下所示:
ng-checked="item.selected"
您不应将ng-model与ng-checked结合使用。来自文档:
请注意,此指令不应与ngModel一起使用 这可能会导致意外行为。
当然,您也可以将所选项目添加到selections
数组,而不是在每个项目上添加selected
标记。我更喜欢这种解决方案,因为它消除了不必要的冗余。
确保将selections
设置为开始的[]
并添加每个所选项目:
if (selectedvalue.indexOf(serverData[i]) >= 0 || null) {
selection.push(modal);
}