|
老大们,新手求助:请问we过本地的json文件获取数据总是获取的是promise对象,导致无法正常解析显示。
用的是ionic tab的模板,把service.js的数据改成了http得get获取,代码如下:
- ngular.module('starter.services', [])
- .factory('Chats', function ($http) {
- // Might use a resource here that returns a JSON array
- //alert("1");
- // Some fake testing data
- /*var chats = [{
- id: 0,
- name: 'Ben Sparrow11',
- lastText: 'You on your way?',
- face: 'img/ionic.png'
- }, {
- id: 1,
- name: 'Max Lynx',
- lastText: 'Hey, it\'s me',
- face: 'https://avatars3.githubusercontent.com/u/11214?v=3&s=460'
- },{
- id: 2,
- name: 'Adam Bradleyson',
- lastText: 'I should buy a boat',
- face: 'http://a.hiphotos.baidu.com/baike/c0%3Dbaike80%2C5%2C5%2C80%2C26/sign=ab55cb8362d9f2d3341c2cbdc885e176/9c16fdfaaf51f3de642e4d0694eef01f3a297930.jpg'
- }, {
- id: 3,
- name: 'Perry Governor',
- lastText: 'Look at my mukluks!',
- face: 'img/ionic.png'
- }, {
- id: 4,
- name: 'Mike Harrington',
- lastText: 'This is wicked good ice cream.',
- face: 'https://pbs.twimg.com/profile_images/578237281384841216/R3ae1n61.png'
- }];*/
- var chats = null;
- var url = "js/data";
- chats = $http.get(url).success(function (response) {
- chats = response;
- console.log("1");
- console.log(response);
- return response;
- });
- console.log("2");
- console.log(chats);
- return {
- all: function () {
- console.log("3");
- console.log(chats);
- return chats;
- },
- remove: function (chat) {
- chats.splice(chats.indexOf(chat), 1);
- },
- get: function (chatId) {
- console.log('get'+chatId+" "+chats.length);
- for (var i = 0; i < chats.length; i++) {
- if (chats[i].id === parseInt(chatId)) {
- console.log('get'+chats[i].name);
- return chats[i];
- }
- }
- return null;
- }
- };
- //return $http.get(url);
- });
复制代码 打印log的时候,发现log2->log3->log1这种顺序打印的,明明我得get函数先写的,为何log1最后执行。而且get函数外部的chats都是promise类型,无法解析显示。
跪谢!!!!
|
|