【发布时间】: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