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.我们可以比 ...
随机推荐
- Eclipse中构建Fluent风格到Formatter
The place to set this is on the "Line Wrapping" tab of the code formatting preferences pag ...
- CSS如何实现数字分页效果
代码实例如下: <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www ...
- 创建指定日期java Date对象
import java.text.DateFormat;import java.text.ParseException;import java.text.SimpleDateFormat;import ...
- oracle修改列的类型
alter table table_name modify column_name datatype;
- C++学习30 重载++和--(自增自减运算符)
自增“++”和自减“--”都是一元运算符,它的前置形式和后置形式都可以被重载.请看下面的例子: #include <iostream> #include <iomanip> u ...
- ElasticSearch 常用的查询过滤语句
query 和 filter 的区别请看: http://www.cnblogs.com/ghj1976/p/5292740.html Filter DSL term 过滤 term主要用于精确匹配 ...
- jquery ui datepicker中文显示
$.datepicker.regional['zh-CN'] = { closeText: '关闭', prevText: '<上月', nextText: '下月>', currentT ...
- Spring MVC 中的基于注解的 Controller【转】
原文地址:http://my.oschina.net/abian/blog/128028 终于来到了基于注解的 Spring MVC 了.之前我们所讲到的 handler,需要根据 url 并通过 H ...
- BC之Run
Problem Description AFA is a girl who like runing.Today,he download an app about runing .The app can ...
- springMVC之HelloWorld
一.总结 1.web项目一定要把引用的jar包放在WEB-INF/lib下(common-logging1.2,spring4.1.6所有包,其实不需要那么多,懒得筛选了,) 2.web.xml中要初 ...