目前我不知道怎么在app.controller里面實現(xiàn)定時發(fā)送請求刷新的方法
所以腕柜,我使用了ajax和angular來實現(xiàn)定時的功能队塘,ajax用來發(fā)送異步請求穴张,angular用來在頁面回顯值
//定義了一個全局變量scopeI撬统,用來接收后臺傳入的json
scopeI=null;
var app = angular.module('myApp', []);
app.controller("myInfoCtrl", function ($scope) {
scopeI =$scope;
});
function myInfo(){
//使用ajax異步請求拿到后臺傳入的json
$.ajax({
async: true,
type: "post",
url: '這里是url地址',
dataType: "json",
success: function (successdata) {
//console.log(successdata);
//scope進行一下apply巩剖,給它賦值
scopeI.$apply(function () {
scopeI.myInfodata = successdata;
});
}
});
};
//頁面加載時調(diào)用該方法
myInfo();
//設(shè)置定時
var timeOut;
//清除定時铝穷,頁面不延遲加載
clearInterval(timeOut);
//設(shè)置3秒的定時請求
setInterval(myInfo,3000);