【问题标题】:How to open specific ion-item in a ion-list如何在离子列表中打开特定的离子项目
【发布时间】:2015-06-10 04:12:04
【问题描述】:

我有一个简单的 Todo 应用程序,它是使用 ionic 框架为练习而构建的。我第一次在 app.service 中完成了这样的任务:

var todos = [
  {title: "Take out the trash", done: 1},
  {title: "Do laundry", done: 0},
  {title: "Start cooking dinner", done: 1}
]

它工作正常。我有一个离子列表,可以打开任何项目并通过复选框更改其“完成”状态。

这是我的离子列表

    <ion-list>
      <ion-item ng-repeat="todo in todos"
        class="item item-icon-right"
       ui-sref="todos.detail({todo: $index})">
         <span ng-class="{done: todo.done}">{{todo.title}}</span>
     </ion-item>
    </ion-list>

一切正常。比我决定从数据库中填充我的 todos 数组。让它在控制器中像这样工作

     $http.get("http://localhost/test.php")
     .success(function (response) {
       $scope.todos = response.records;
       for(var i=0; i<$scope.todos.length; i++){
         if($scope.todos[i].done == 1){
             $scope.todos[i].done = true;
         }else{
            $scope.todos[i].done = false;
         }
       }
      }); 

它有效,我仍然可以列出 ion-list 中的项目,但现在我无法打开任何项目。

我认为需要做一些事情

   ui-sref="todos.detail({todo: $index})

但我不知道是什么。

编辑 我的 todos.detail 状态提供者是

   $stateProvider.state('todos.detail', {
    url: '/:todo',
    templateUrl: 'todo.html',
    controller: 'TodoCtrl',
    resolve: {
        todo: function($stateParams, TodosService) {
        return TodosService.getTodo($stateParams.todo)
        }
    }
  })

也许这就是问题所在,不再使用 TodosService

   app.service('TodosService', function($http) {
   var todos = []
   return {
     todos: todos,
     getTodo: function(index) {
       return todos[index]
       }
    }
  })

【问题讨论】:

  • 能否请您展示一些您的回复:response.records 您的问题应该在这里 ui-sref 没问题 IMO,$index 参考 ng-repeat
  • 我不明白,也许你想看这个。它来自 test.php {"records":[{"title":"Test 1","done":"1","id":"1"},{"title":"Test 2"," done":"0","id":"2"},{"title":"Test 3","done":"1","id":"3"}]}

标签: javascript angularjs angular-ui-router ionic-framework


【解决方案1】:

让它工作。我需要像这样填充在 TodosServices 中返回的数组

   app.service('TodosService', function($http) {
   var todos = []
   $http.get("http://localhost/test.php")
   .success(function (response) {
     todos = response.records;
     for(var i=0; i<todos.length; i++){
        if(todos[i].done == 1){
            todos[i].done = true;
        }else{
            todos[i].done = false;
        }

    }
   });   
  return {
    todos: todos,
   getTodo: function(index) {
   return todos[index]
 }
  }
 })

【讨论】:

    猜你喜欢
    • 2019-01-19
    • 2022-01-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多