我目前正在学习Ionic和AngularJS。我目前正在调整一个在他们的'教程网站'上完美工作的代码,但不是在我的代码中。
下面是我想要做的:我有一个联系人列表显示一些信息,当我点击一个项目时,我想去关联的聊天。
这里有规定:
这里有两个控制器:
.controller('ContactsController', function($scope, $state, $stateParams, ChatService) {
$scope.toDiscover = function(){
$state.go('discover');
};
$scope.toDiscussion = function(user_id1, user_id2){
$state.go('conversation', {
id1: user_id1,
id2: user_id2
});
};
$scope.chats = ChatService.getChats();
})
.controller('ConversationController', function($scope, $state, $stateParams, ChatService) {
$scope.toContacts = function(){
$state.go('contacts')
};
$scope.chatId = $stateParams.chatId;
$scope.chat = ChatService.getChat($scope.chatId);
})
这里就是所谓的服务:
.service('ChatService', function(){
return {
chats: [
{
id: "1",
message: "Chat Message 1"
},
{
id: "2",
message: "Chat Message 2"
},
{
id: "3",
message: "Chat Message 3"
},
{
id: "4",
message: "Chat Message 4"
},
{
id: "5",
message: "Chat Message 5"
}
],
getChats: function(){
return this.chats;
},
getChat: function(chatId) {
for(i=0; i<this.chats.length; i++){
if(this.chats[i].id == chatId){
return this.chats[i];
}
}
}
}
})
和有关网页(联系人):
<ion-view view-title="Contacts" ng-controller="ContactsController">
<ion-content class="content-img" scroll="" class="padding">
<div class="under-header"></div>
<a ng-repeat="chat in chats" class="item item-avatar" ng-click="toDiscussion({{chat.id}}, {{chat.id}})">
<img src="img/launchscreen.png">
<h2><span class="contact-name"> ({{ chat.id }})</span></h2>
<p class="preview">Back off, man. I'm a scientist.</p>
</a>
</ion-content>
对话:
<ion-content class="content-img" scroll="" class="padding">
<div class="under-header"></div>
<ion-item><b>Chat:</b> {{ chat.id }}</ion-item>
<ion-item><b>Message:</b> {{ chat.message }}</ion-item>
</ion-content>
<div class="bar bar-footer bar-light cheam-chatbar" keyboard-attach>
<div class="title"><input type="text" name="chatbox" placeholder="send message to"></div>
</div>
所以这里的问题:当在谈话中,我可以访问德想要的信息没有任何问题。当我点击一个项目以查看关联的聊天信息时,没有信息显示。
我已经在他们的网站和我的IDE中的代码,似乎没有发现问题。
有人可以解释为什么会发生这种情况?
谢谢! (对不起,对角的新手问题)
感谢您的信息! 我所有的状态都被很好的定义了,我把'$ location'方法改为'$ state.go()'方法。 但我仍然无法检索到会话中的信息,这是否正常? – TanguyH
绝对不正常!;)你可以添加你的路由定义到你的文章?然后尝试准备一个工作示例。如果你不想等我或者只是想自己做,你也可以做--Ionic Playground非常适合准备和分享快速示例。 http://play.ionic.io/通常这是检测问题的最简单方法! – OClyde
我添加了状态和路由,以便您可以看到。不知何故,我可以检索信息,因为我的对话链接是:'/#/ conversation/1/1',但下面的信息不显示。也许我在这里错过了一些东西? – TanguyH