[英]How to open specific ion-item in a ion-list
I have simple Todo app that was build for practice using ionic framework. 我有一个简单的Todo应用程序,用于使用离子框架进行练习。 For the first time i filled my tasks like this inside app.service : 我第一次在app.service中完成了这样的任务:
var todos = [
{title: "Take out the trash", done: 1},
{title: "Do laundry", done: 0},
{title: "Start cooking dinner", done: 1}
]
It works fine. 工作正常。 I have an ion-list an can open any item and change its "done" status via check box. 我有一个离子列表,可以打开任何项目并通过复选框更改其“完成”状态。
This is my ion-list 这是我的离子清单
<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>
Everything worked fine. 一切正常。 Than I decided to fill my todos array from database. 然后我决定从数据库中填充待办事项数组。 Made it work like this inside a controller 使它在控制器内像这样工作
$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;
}
}
});
It works, I still can list out items in ion-list, but now I am unable to open any of items. 它有效,我仍然可以在ion-list中列出项目,但是现在我无法打开任何项目。
I think something needs to be done with 我认为需要做些事情
ui-sref="todos.detail({todo: $index})
but I do not know what. 但我不知道
EDIT My state provider for todos.detail is 编辑我的todos.detail的状态提供者是
$stateProvider.state('todos.detail', {
url: '/:todo',
templateUrl: 'todo.html',
controller: 'TodoCtrl',
resolve: {
todo: function($stateParams, TodosService) {
return TodosService.getTodo($stateParams.todo)
}
}
})
Maybe that's the problem becouse, TodosService is not used anymore 也许这就是问题所在,因为不再使用TodosService
app.service('TodosService', function($http) {
var todos = []
return {
todos: todos,
getTodo: function(index) {
return todos[index]
}
}
})
Made it work. 使它工作。 I needed to fill an array that was returned in TodosServices like this 我需要像这样在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]
}
}
})
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.