ionic 向後台請求json 數據 在頁面上的顯示問題
我向服務器請求數據,獲取到的數據竟然不能顯示在頁面上 我那個氣啊.....
<ul>
<!-- <li ng-repeat="phone in phones">
{{phone.name}}
<p>{{phone.snippet}}</p>
</li> -->
<li ng-repeat="x in names">
{{ x.Name + ', ' + x.Country }}
</li>
</ul>
頁面上綁定了數據
創建服務 HomeExchangeList
.service('HomeExchangeList', function($rootScope, $http, $log) {
this.getHomeExchange = function() {
var rates = $http({
method: 'GET',
url: 'http://www.w3schools.com//website/Customers_JSON.php'
}).success(function(data) {
$log.log(data);
// removed your return data; it doesn't do anything, and this success is only added to log the result. if you don't need the log other than for debugging, get rid of this success handler too.
});
return rates;
};
})
在控制器中 調用數據
.controller('DashCtrl', function($scope, HomeExchangeList) {
HomeExchangeList.getHomeExchange().success(function(data) {
$scope.names = data
});
})
注:
這裏的控制器 注入了HomeExchangeList 服務 ;然後調用服務HomeExchangeList 的getHomeExchange()函數 ,在回調函數.success(function(data){ })裏面綁定數據 只有這樣才能顯示
2. 另外一種
創建服務 phoneService
.factory('phoneService', function($http, $q) {
return {
getPhones: function() {
var deferred = $q.defer();
$http.get('http://www.w3schools.com//website/Customers_JSON.php').success(function(data) {
console.log('success');
deferred.resolve(data);
}).error(function(){
console.log('error');
deferred.reject();
});
return deferred.promise;
}
}
})
創建控制器
.controller('DashCtrl2', function($scope,phoneService) {
phoneService.getPhones().then(function(data) {
$scope.names = data;
});
})
注:
服務phoneService 中有promise 的影響 ;所以在控制器中注入phoneService 服務時 ,調用服務的函數 需要 .then(function(data){}) 裏面綁定數據
3. 還有這種
.factory('Recipe',['$resource',function($resource){
return $resource('http://www.w3schools.com//website/Customers_JSON.php');
}])
.factory('loadRecipes',['Recipe','$q',function(Recipe,$q){
return function(){
var defer = $q.defer();
Recipe.query(function(recipes){
defer.resolve(recipes)
},function(){
defer.reject();
});
return defer.promise;
}
}]);
控制器為
.controller('DashCtrl1', function($scope,loadRecipes) {
loadRecipes().then(function(data) {
$scope.names = data;
});
})
這種跟第二種一樣的原理......
ionic 向後台請求json 數據 在頁面上的顯示問題的更多相关文章
- 將後台的Json數據返回到前台
前台JS代碼 $.post('/Book/GetBookClassIDByName', { BookName: "旅遊手冊" }, function (data) { if (da ...
- jquery easyui 1.3.4 datagrid pageNumber 設置導致兩次請求的解决方案
$('#table').datagrid({ url: '/get/data/path/to/your/server', pageNumber: , pageSize: , ... }); 當手動設置 ...
- 「Ionic」使用chrom時的跨域問題
前言:在angularjs請求數據時,會遇到跨域問題,解決辦法有很多,但是都不是我想要的(很多人云亦云,都解決不了問題).如果你只是想在本機測試使用,可以參考如下設置. 具體辦法: 1.在电脑上新 ...
- PHP CURL header 设置HOST主机头进行访问并 POST提交數據
$host = array("Host: act.qzone.qq.com");// 域名不帶http://$data = array( 'aa' => ...
- Python 基礎 - 數據類型
標準數據類型 Python3 中有六個標準的數據類型 1 Number(數字) 2 String(字符串) 3 List (列表) 4 Tuple (元組) 5 Sets (集合) 6 Diction ...
- JQuery返回Json日期格式的問題
用JQuery Ajax返回一個Entity的Json數據時,如果Entity的屬性中有日期格式,那返回來的是一串字符串,如下圖所示: 在網上找了很久也沒有找到一個好的解決方案,最後自己寫一個java ...
- (原創) 如何在Nios II顯示8位數的七段顯示器? (SOC) (Nios II) (SOPC Builder) (DE2-70)
Abstract本文討論如何在Nios II控制8位數的七段顯示器. Introduction使用環境:Quartus II 8.0 + Nios II EDS 8.0 + DE2-70 (Cyclo ...
- Grafana展示報表數據的配置(二)
一.Grafana以圖表的形式展示KPI報表的結果數據1.按照日期顯示數據達標量與未達標量2.顯示當前報表的最大值.最小值.平均值.總量3.報表結果數據的鏈接分享與頁面嵌入,用戶無需登錄直接訪問報表統 ...
- Flutter-網絡請求
Flutter 请求网络的三种方式 flutter 请求网络的方式有三种,分别是 Dart 原生的网络请求 HttpClient.第三方网络请求 http以及 Flutter 中的 Dio.我们可以比 ...
随机推荐
- 在UIViewController中获得Container View里的embed viewController的引用
When you want to use a controller you use the UIStoryboard method instantiateViewControllerWithIdent ...
- Asp.net MVC 中Controller返回值类型ActionResult
[Asp.net MVC中Controller返回值类型] 在mvc中所有的controller类都必须使用"Controller"后缀来命名并且对Action也有一定的要求: 必 ...
- Redis常用方法
首先构建非切片连接池jedisPool对象,写好配置redis连接的方法. /** * 构建redis切片连接池 * * @param ip * @param port * @return Jedis ...
- PLSQL_基础系列08_操作符标LPAD / TRUNC / DECODE / TRIM / INSTR(案例)
2014-12-09 Created By BaoXinjian
- Studio--代理设置(SDK下载代理设置)
为啥Android Studio有代理一说呢.比如我们要下载某个插件,但是这个插件又被tc墙了,所以这个时候需要FQ才能安装.FQ其中的一种方式就是使用VPN,配置如下图: 输入VPN的IP和PORT ...
- 转--Android中调用webservice的工具类
最近学习WebService,感觉利用这个借口开发网站的Android客户端方便及了,用到一个工具类,这里铭记一下. public static final String WebServiceName ...
- oProfile 学习
oProfile工具可以分析CPU的负载量 只要对目标程序加上 -g 后重新编译,即可用oProfile进行分析 例如在测试apache的性能时, 增加 -g 编译选项[crifan@localhos ...
- Python标准库——走马观花
作者:Vamei 出处:http://www.cnblogs.com/vamei 欢迎转载,也请保留这段声明.谢谢! Python有一套很有用的标准库(standard library).标准库会随着 ...
- Async/Await - Best Practices in Asynchronous Programming z
These days there’s a wealth of information about the new async and await support in the Microsoft .N ...
- windows service的作成
http://jingyan.baidu.com/article/fa4125acb71a8628ac709226.html