用angularJs轮询

时间:2014-09-11 12:19:26

标签: php angularjs polling

我只想为我的客户端的当前更新建立轮询连接。

我无法使其发挥作用:

var app = angular.module('url', []);

app.factory('Poller', function($http,$q){
    return {
        poll : function(api){
            var deferred = $q.defer();
            $http.get(api).then(function (response) {
                deferred.resolve(response.data);
            });
            return deferred.promise;
        }

    }
});

app.directive('ngBlur', function(){
    return function(scope, elem, attrs){
        elem.bind('blur', function(){
            scope.$apply(attrs.ngBlur);
        })
    }
});

app.controller('UrlCtrl', function($scope, $filter, $http, $location, Poller){

    $scope.myts = Poller.poll('localhost:8888/mytest.php');
    $scope.mydate = $scope.myts.then(function(data){
        return $filter('date')(data,'yyyy-MM-dd HH:mm:ss Z');
    });
    var Repeater = function () {
        $scope.$apply(function () {
            $scope.myts = Poller.poll('localhost:8888/mytest.php');
            $scope.mydate = $scope.myts.then(function(data){
                return $filter('date')(data,'yyyy-MM-dd HH:mm:ss Z');
            });
        });
    };
    var timer = setInterval(Repeater, 1000);


    $scope.urls = [];

    $http.get("http://localhost:8888/web-service.php")
        .success(function(data) {
            data.URLs.forEach(function(url){
                $scope.urls.push({
                    name : url.name,
                    preferred : false
                });

            })
        })

    $scope.$watch('urls', function(){
        $scope.chosenURL = $filter('filter')($scope.urls, {preferred:true}).length;
        $scope.allchecked = !$filter('filter')($scope.urls, {preferred:false}).length;
    }, true)

    if($location.path() == ''){ $location.path('/')}
    $scope.location = $location;
    $scope.$watch('location.path()', function(path){
        $scope.statusFilter =
            (path == '/active') ? {preferred : true} :
            null;
    });

    $scope.removeUrl = function(index){
        $scope.urls.splice(index,1);
    }

    $scope.addUrl = function(){
        $scope.urls.push({
            name : $scope.newurl,
            preferred : false
        });
        $scope.newurl = '';
    }

    $scope.editUrl = function(url){
        url.editing = false;
    }

    $scope.checkAllUrl = function(allchecked){
        $scope.urls.forEach(function(url){
            url.preferred = allchecked;
        })
    };
});

mytest.php

<?php 
header('Access-Control-Allow-Origin: *');
header("Access-Control-Allow-Headers: Origin, X-Requested-With, Content-Type, Accept");
header('Access-Control-Allow-Methods: POST, GET, OPTIONS');
echo time()*1000;

?>

这是我得到的错误:

  

无法加载资源:net :: ERR_UNKNOWN_URL_SCHEME localhost:8888/mytest.php无法加载资源:服务器   回复状态为404(未找到)   http://localhost:63342/DeliciousJS/js/app.js.map无法加载   资源:服务器响应状态为404(未找到)   http://ajax.googleapis.com/ajax/libs/angularjs/1.0.3/angular.min.js.map   8无法加载资源:net :: ERR_UNKNOWN_URL_SCHEME   localhost:8888/mytest.php选项localhost:8888/mytest.php   net :: ERR_UNKNOWN_URL_SCHEME angular.min.js:97(匿名函数)   angular.min.js:97o angular.min.js:93l angular.min.js:92l。(匿名   function)angular.min.js:94poll app.js:7(匿名函数)   app.js:32e。$ eval angular.min.js:86e。$ apply angular.min.js:86Repeater   app.js:31个选项localhost:8888/mytest.php   净:: ERR_UNKNOWN_URL_SCHEME

为什么我直接尝试使用Chrome时找不到正常工作的网址?

感谢您的帮助

0 个答案:

没有答案