首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何在数据库中选中ion-checkbox

如何在数据库中选中ion-checkbox
EN

Stack Overflow用户
提问于 2016-03-18 16:23:19
回答 3查看 1.1K关注 0票数 2

我已经填充了这个离子标签,并且所有项目都未选中:

代码语言:javascript
复制
            <ion-checkbox ng-repeat="categoria in listaCategorias"
                           ng-model="categoria.checked"
                           ng-checked="categoria.checked"
                           ng-change="recuperarServicos(categoria)">
                           {{ categoria.nmCategoria }}
            </ion-checkbox>

下面是我的控制器代码,它有一个“categoria”列表:

代码语言:javascript
复制
//here I have the ids recovered from database that I split into an array of ids
var idsCategoria = $scope.meuanuncio.idsCategoria.trim().split(',');

if($scope.listaCategorias.length > 0)
{
    //for each item in my listaCategorias (used in ng-repeat)
    for (var i = 0; i < $scope.listaCategorias.length; i++) {

        var item = $scope.listaCategorias[i];

        //I compare id from each item with my list recovered from database
        if(idsCategoria.indexOf($scope.listaCategorias[i].idCategoria) != -1)
        {
            //If the item id exist in database list, I check the item
            item.checked = true;

            // Below there are other ways that I tried to use
            // $scope.listaCategorias[i].Selected = true;
            // $scope.listaCategorias[i].checked = true;
            $scope.listaCategorias[0].checked = true;
        }
    }
};

但我不能选中我的离子复选框项目。

我做错了什么?

谢谢。

EN

回答 3

Stack Overflow用户

发布于 2016-03-18 23:52:30

代码语言:javascript
复制
ng-model="categoria.checked"

看起来很好,但是不需要检查ng。

代码语言:javascript
复制
 var item = $scope.listaCategorias[i];
item.checked = true;

不,物品会在循环中丢失。我看到你在试着:

代码语言:javascript
复制
$scope.listaCategorias[i].checked = true;

你是遇到错误了还是怎么的?因为这看起来像是这样做的。

也许可以尝试在ion-checkbox周围的div上进行循环?又名

代码语言:javascript
复制
<div ng-repeat="categoria in listaCategorias">
   <ion-checkbox ng-model="categoria.checked"
                 ng-change="recuperarServicos(categoria)">
                 {{ categoria.nmCategoria }}
   </ion-checkbox> 
</div>
票数 1
EN

Stack Overflow用户

发布于 2016-03-19 02:22:52

试试这个:

代码语言:javascript
复制
<div ng-repeat="categoria in listaCategorias track by $index">
      <ion-item class="item item-checkbox">
           <label class="checkbox">
              <input type="checkbox" ng-model="categoria.checked" ng-change="recuperarServicos(categoria)">
           </label>
         {{categoria.nmCategoria}}
      </ion-item>
</div>

控制器:

代码语言:javascript
复制
$scope.recuperarServicos = function(categoria){
        if(categoria.selected && ($scope.selectedItems.indexOf(categoria.name) < 0)){
          $scope.selectedItems.push(categoria.name);
        }else{
            $scope.selectedItems.splice($scope.selectedItems.indexOf(categoria.name), 1);
        }
    };

希望这能以某种方式帮助you..in。

票数 1
EN

Stack Overflow用户

发布于 2016-03-19 11:21:33

我的问题是当我将我的项数组归于$scope.listaCategorias时。

我就是这么做的:

$scope.listaCategorias = listaCategorias;

但我需要这样做:

$scope.listaCategorias.push.apply($scope.listaCategorias,listaCategorias);

我正在构建一个内部带有checked属性的数组,但是当我关联我的构建列表时,我关联的是第一个没有设置checked属性的数组。

现在让我展示一下我的代码。

我的观点是:

代码语言:javascript
复制
        <div ng-repeat="item in listaCategorias track by $index">
              <ion-item class="item item-checkbox">
                   <label class="checkbox">
                      <input type="checkbox" ng-model="item.checked" ng-checked="item.checked" ng-change="recuperarServicos(item)">
                   </label>
                  {{ item.nmCategoria }}
              </ion-item>
        </div> 

我的控制器:

代码语言:javascript
复制
//here I get all my 'categorias' from datatable
listaCategorias = appFactory.recuperarCategorias();

//If list is not null go ahead   
if(listaCategorias != null) {

    //split into an array all my 'categoria' ids
    var idsCategoria = $scope.meuanuncio.idsCategoria.split(',');

    //if list has items go ahead
    if(listaCategorias.length > 0) {

        for (var i = 0; i < listaCategorias.length; i++) {

            //if 'categoria' id exists in datatable list set true, else false
            if(idsCategoria.indexOf(listaCategorias[i].idCategoria) != -1) {
                listaCategorias[i].checked = true;
            }
            else {
                listaCategorias[i].checked = false;
            }
        }
    };

    //Here is the point !!! I need to load my $scope variable this way to build all my items correctly
    $scope.listaCategorias.push.apply($scope.listaCategorias, listaCategorias);

}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/36079416

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档