Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angularjs/22.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用javascript和AngularJS检测重写规则背后是否存在资源_Javascript_Angularjs - Fatal编程技术网

使用javascript和AngularJS检测重写规则背后是否存在资源

使用javascript和AngularJS检测重写规则背后是否存在资源,javascript,angularjs,Javascript,Angularjs,我正在尝试只使用javascript和AngularJS覆盖我的单页应用程序的某些部分 覆盖基于子域 每个子域都指向同一个文档根 controllers.js controller('AppController', ['$scope','$route','$routeParams','$location', function($scope, $route, $routeParams, $location) { $scope.$on("$routeChangeSuccess",func

我正在尝试只使用javascript和AngularJS覆盖我的单页应用程序的某些部分

覆盖基于子域

每个子域都指向同一个文档根

controllers.js

controller('AppController', ['$scope','$route','$routeParams','$location', function($scope, $route, $routeParams, $location) {    
  $scope.$on("$routeChangeSuccess",function( $currentRoute, $previousRoute ){
    render();
  });
  var render = function(){
    //Is it actually a subdomain?
    if($location.host().split(".",1).length>2){ 
      //Use subdomain folder if it is.
      var url = "views/"+$location.host().split(".",1)+"/"+$route.current.template;
      var http = new XMLHttpRequest();    
      http.onreadystatechange=function(){
        if (http.readyState==4){
          //If there isn't an overwrite, use the original.
          $scope.page = (http.status!=404)?url:("views/"+$route.current.template);
        }
      }
      http.open('HEAD', url, true);
      http.send();
    }
    else{
      //Else we are on the parent domain.
      $scope.page  = "views/"+$route.current.template;
    }
  };
}])
config(['$routeProvider', '$locationProvider', function($routeProvider, $locationProvider) {
  $locationProvider.html5Mode(true);
  $routeProvider.when('/', {
    template: 'home.html'
  });
  $routeProvider.when('/services', {
    template: 'services.html'
  });
  $routeProvider.otherwise({redirectTo: '/'});
}]);
factory('Views', function($location,$route,$routeParams,objExistsFilter) {

  var viewsService = {};
  var views = {
    subdomain1:{
      'home.html':'/views/subdomain1/home.html'
      },
    subdomain2:{

    },
    'home.html':'/views/home.html',
  };

  viewsService.returnView = function() {
    var y = $route.current.template;
    var x = $location.host().split(".");
    return (x.length>2)?(objExistsFilter(views[x[0]][y]))?views[x[0]][y]:views[y]:views[y];
  };

  viewsService.returnViews = function() {
    return views;
  };

  return viewsService;
}).
controller('AppController', ['$scope','Views', function($scope, Views) {    
  $scope.$on("$routeChangeSuccess",function( $currentRoute, $previousRoute ){
    $scope.page = Views.returnView();
  });
}]).
filter('objExists', function () {
  return function (property) {
    try {
      return property;
    } catch (err) {
      return null
    }
  };
});
config.js

controller('AppController', ['$scope','$route','$routeParams','$location', function($scope, $route, $routeParams, $location) {    
  $scope.$on("$routeChangeSuccess",function( $currentRoute, $previousRoute ){
    render();
  });
  var render = function(){
    //Is it actually a subdomain?
    if($location.host().split(".",1).length>2){ 
      //Use subdomain folder if it is.
      var url = "views/"+$location.host().split(".",1)+"/"+$route.current.template;
      var http = new XMLHttpRequest();    
      http.onreadystatechange=function(){
        if (http.readyState==4){
          //If there isn't an overwrite, use the original.
          $scope.page = (http.status!=404)?url:("views/"+$route.current.template);
        }
      }
      http.open('HEAD', url, true);
      http.send();
    }
    else{
      //Else we are on the parent domain.
      $scope.page  = "views/"+$route.current.template;
    }
  };
}])
config(['$routeProvider', '$locationProvider', function($routeProvider, $locationProvider) {
  $locationProvider.html5Mode(true);
  $routeProvider.when('/', {
    template: 'home.html'
  });
  $routeProvider.when('/services', {
    template: 'services.html'
  });
  $routeProvider.otherwise({redirectTo: '/'});
}]);
factory('Views', function($location,$route,$routeParams,objExistsFilter) {

  var viewsService = {};
  var views = {
    subdomain1:{
      'home.html':'/views/subdomain1/home.html'
      },
    subdomain2:{

    },
    'home.html':'/views/home.html',
  };

  viewsService.returnView = function() {
    var y = $route.current.template;
    var x = $location.host().split(".");
    return (x.length>2)?(objExistsFilter(views[x[0]][y]))?views[x[0]][y]:views[y]:views[y];
  };

  viewsService.returnViews = function() {
    return views;
  };

  return viewsService;
}).
controller('AppController', ['$scope','Views', function($scope, Views) {    
  $scope.$on("$routeChangeSuccess",function( $currentRoute, $previousRoute ){
    $scope.page = Views.returnView();
  });
}]).
filter('objExists', function () {
  return function (property) {
    try {
      return property;
    } catch (err) {
      return null
    }
  };
});
index.html

<html lang="en" ng-app="myApp" ng-controller="AppController">
<body>
<div ng-include src="page" class="container"></div>
</body>
</html>
当我不在子域上时,这非常有效,但我也没有发送
XMLHttpRequest
。当我使用子域时,现在我们需要检查覆盖

这里棘手的部分是重写规则迫使XMLHttpRequest返回一个200。


关于如何既吃蛋糕又吃蛋糕的想法?

出于两个原因,我决定选择当地的斯特拉德吉餐厅:

  • XML请求没有额外的开销
  • 404当资源不存在时,消息不会发送控制台日志
  • services.js

    controller('AppController', ['$scope','$route','$routeParams','$location', function($scope, $route, $routeParams, $location) {    
      $scope.$on("$routeChangeSuccess",function( $currentRoute, $previousRoute ){
        render();
      });
      var render = function(){
        //Is it actually a subdomain?
        if($location.host().split(".",1).length>2){ 
          //Use subdomain folder if it is.
          var url = "views/"+$location.host().split(".",1)+"/"+$route.current.template;
          var http = new XMLHttpRequest();    
          http.onreadystatechange=function(){
            if (http.readyState==4){
              //If there isn't an overwrite, use the original.
              $scope.page = (http.status!=404)?url:("views/"+$route.current.template);
            }
          }
          http.open('HEAD', url, true);
          http.send();
        }
        else{
          //Else we are on the parent domain.
          $scope.page  = "views/"+$route.current.template;
        }
      };
    }])
    
    config(['$routeProvider', '$locationProvider', function($routeProvider, $locationProvider) {
      $locationProvider.html5Mode(true);
      $routeProvider.when('/', {
        template: 'home.html'
      });
      $routeProvider.when('/services', {
        template: 'services.html'
      });
      $routeProvider.otherwise({redirectTo: '/'});
    }]);
    
    factory('Views', function($location,$route,$routeParams,objExistsFilter) {
    
      var viewsService = {};
      var views = {
        subdomain1:{
          'home.html':'/views/subdomain1/home.html'
          },
        subdomain2:{
    
        },
        'home.html':'/views/home.html',
      };
    
      viewsService.returnView = function() {
        var y = $route.current.template;
        var x = $location.host().split(".");
        return (x.length>2)?(objExistsFilter(views[x[0]][y]))?views[x[0]][y]:views[y]:views[y];
      };
    
      viewsService.returnViews = function() {
        return views;
      };
    
      return viewsService;
    }).
    
    controller('AppController', ['$scope','Views', function($scope, Views) {    
      $scope.$on("$routeChangeSuccess",function( $currentRoute, $previousRoute ){
        $scope.page = Views.returnView();
      });
    }]).
    
    filter('objExists', function () {
      return function (property) {
        try {
          return property;
        } catch (err) {
          return null
        }
      };
    });
    
    controllers.js

    controller('AppController', ['$scope','$route','$routeParams','$location', function($scope, $route, $routeParams, $location) {    
      $scope.$on("$routeChangeSuccess",function( $currentRoute, $previousRoute ){
        render();
      });
      var render = function(){
        //Is it actually a subdomain?
        if($location.host().split(".",1).length>2){ 
          //Use subdomain folder if it is.
          var url = "views/"+$location.host().split(".",1)+"/"+$route.current.template;
          var http = new XMLHttpRequest();    
          http.onreadystatechange=function(){
            if (http.readyState==4){
              //If there isn't an overwrite, use the original.
              $scope.page = (http.status!=404)?url:("views/"+$route.current.template);
            }
          }
          http.open('HEAD', url, true);
          http.send();
        }
        else{
          //Else we are on the parent domain.
          $scope.page  = "views/"+$route.current.template;
        }
      };
    }])
    
    config(['$routeProvider', '$locationProvider', function($routeProvider, $locationProvider) {
      $locationProvider.html5Mode(true);
      $routeProvider.when('/', {
        template: 'home.html'
      });
      $routeProvider.when('/services', {
        template: 'services.html'
      });
      $routeProvider.otherwise({redirectTo: '/'});
    }]);
    
    factory('Views', function($location,$route,$routeParams,objExistsFilter) {
    
      var viewsService = {};
      var views = {
        subdomain1:{
          'home.html':'/views/subdomain1/home.html'
          },
        subdomain2:{
    
        },
        'home.html':'/views/home.html',
      };
    
      viewsService.returnView = function() {
        var y = $route.current.template;
        var x = $location.host().split(".");
        return (x.length>2)?(objExistsFilter(views[x[0]][y]))?views[x[0]][y]:views[y]:views[y];
      };
    
      viewsService.returnViews = function() {
        return views;
      };
    
      return viewsService;
    }).
    
    controller('AppController', ['$scope','Views', function($scope, Views) {    
      $scope.$on("$routeChangeSuccess",function( $currentRoute, $previousRoute ){
        $scope.page = Views.returnView();
      });
    }]).
    
    filter('objExists', function () {
      return function (property) {
        try {
          return property;
        } catch (err) {
          return null
        }
      };
    });
    
    filters.js

    controller('AppController', ['$scope','$route','$routeParams','$location', function($scope, $route, $routeParams, $location) {    
      $scope.$on("$routeChangeSuccess",function( $currentRoute, $previousRoute ){
        render();
      });
      var render = function(){
        //Is it actually a subdomain?
        if($location.host().split(".",1).length>2){ 
          //Use subdomain folder if it is.
          var url = "views/"+$location.host().split(".",1)+"/"+$route.current.template;
          var http = new XMLHttpRequest();    
          http.onreadystatechange=function(){
            if (http.readyState==4){
              //If there isn't an overwrite, use the original.
              $scope.page = (http.status!=404)?url:("views/"+$route.current.template);
            }
          }
          http.open('HEAD', url, true);
          http.send();
        }
        else{
          //Else we are on the parent domain.
          $scope.page  = "views/"+$route.current.template;
        }
      };
    }])
    
    config(['$routeProvider', '$locationProvider', function($routeProvider, $locationProvider) {
      $locationProvider.html5Mode(true);
      $routeProvider.when('/', {
        template: 'home.html'
      });
      $routeProvider.when('/services', {
        template: 'services.html'
      });
      $routeProvider.otherwise({redirectTo: '/'});
    }]);
    
    factory('Views', function($location,$route,$routeParams,objExistsFilter) {
    
      var viewsService = {};
      var views = {
        subdomain1:{
          'home.html':'/views/subdomain1/home.html'
          },
        subdomain2:{
    
        },
        'home.html':'/views/home.html',
      };
    
      viewsService.returnView = function() {
        var y = $route.current.template;
        var x = $location.host().split(".");
        return (x.length>2)?(objExistsFilter(views[x[0]][y]))?views[x[0]][y]:views[y]:views[y];
      };
    
      viewsService.returnViews = function() {
        return views;
      };
    
      return viewsService;
    }).
    
    controller('AppController', ['$scope','Views', function($scope, Views) {    
      $scope.$on("$routeChangeSuccess",function( $currentRoute, $previousRoute ){
        $scope.page = Views.returnView();
      });
    }]).
    
    filter('objExists', function () {
      return function (property) {
        try {
          return property;
        } catch (err) {
          return null
        }
      };
    });
    

    我决定和当地的斯特拉德吉一起去,原因有两个:

  • XML请求没有额外的开销
  • 404当资源不存在时,消息不会发送控制台日志
  • services.js

    controller('AppController', ['$scope','$route','$routeParams','$location', function($scope, $route, $routeParams, $location) {    
      $scope.$on("$routeChangeSuccess",function( $currentRoute, $previousRoute ){
        render();
      });
      var render = function(){
        //Is it actually a subdomain?
        if($location.host().split(".",1).length>2){ 
          //Use subdomain folder if it is.
          var url = "views/"+$location.host().split(".",1)+"/"+$route.current.template;
          var http = new XMLHttpRequest();    
          http.onreadystatechange=function(){
            if (http.readyState==4){
              //If there isn't an overwrite, use the original.
              $scope.page = (http.status!=404)?url:("views/"+$route.current.template);
            }
          }
          http.open('HEAD', url, true);
          http.send();
        }
        else{
          //Else we are on the parent domain.
          $scope.page  = "views/"+$route.current.template;
        }
      };
    }])
    
    config(['$routeProvider', '$locationProvider', function($routeProvider, $locationProvider) {
      $locationProvider.html5Mode(true);
      $routeProvider.when('/', {
        template: 'home.html'
      });
      $routeProvider.when('/services', {
        template: 'services.html'
      });
      $routeProvider.otherwise({redirectTo: '/'});
    }]);
    
    factory('Views', function($location,$route,$routeParams,objExistsFilter) {
    
      var viewsService = {};
      var views = {
        subdomain1:{
          'home.html':'/views/subdomain1/home.html'
          },
        subdomain2:{
    
        },
        'home.html':'/views/home.html',
      };
    
      viewsService.returnView = function() {
        var y = $route.current.template;
        var x = $location.host().split(".");
        return (x.length>2)?(objExistsFilter(views[x[0]][y]))?views[x[0]][y]:views[y]:views[y];
      };
    
      viewsService.returnViews = function() {
        return views;
      };
    
      return viewsService;
    }).
    
    controller('AppController', ['$scope','Views', function($scope, Views) {    
      $scope.$on("$routeChangeSuccess",function( $currentRoute, $previousRoute ){
        $scope.page = Views.returnView();
      });
    }]).
    
    filter('objExists', function () {
      return function (property) {
        try {
          return property;
        } catch (err) {
          return null
        }
      };
    });
    
    controllers.js

    controller('AppController', ['$scope','$route','$routeParams','$location', function($scope, $route, $routeParams, $location) {    
      $scope.$on("$routeChangeSuccess",function( $currentRoute, $previousRoute ){
        render();
      });
      var render = function(){
        //Is it actually a subdomain?
        if($location.host().split(".",1).length>2){ 
          //Use subdomain folder if it is.
          var url = "views/"+$location.host().split(".",1)+"/"+$route.current.template;
          var http = new XMLHttpRequest();    
          http.onreadystatechange=function(){
            if (http.readyState==4){
              //If there isn't an overwrite, use the original.
              $scope.page = (http.status!=404)?url:("views/"+$route.current.template);
            }
          }
          http.open('HEAD', url, true);
          http.send();
        }
        else{
          //Else we are on the parent domain.
          $scope.page  = "views/"+$route.current.template;
        }
      };
    }])
    
    config(['$routeProvider', '$locationProvider', function($routeProvider, $locationProvider) {
      $locationProvider.html5Mode(true);
      $routeProvider.when('/', {
        template: 'home.html'
      });
      $routeProvider.when('/services', {
        template: 'services.html'
      });
      $routeProvider.otherwise({redirectTo: '/'});
    }]);
    
    factory('Views', function($location,$route,$routeParams,objExistsFilter) {
    
      var viewsService = {};
      var views = {
        subdomain1:{
          'home.html':'/views/subdomain1/home.html'
          },
        subdomain2:{
    
        },
        'home.html':'/views/home.html',
      };
    
      viewsService.returnView = function() {
        var y = $route.current.template;
        var x = $location.host().split(".");
        return (x.length>2)?(objExistsFilter(views[x[0]][y]))?views[x[0]][y]:views[y]:views[y];
      };
    
      viewsService.returnViews = function() {
        return views;
      };
    
      return viewsService;
    }).
    
    controller('AppController', ['$scope','Views', function($scope, Views) {    
      $scope.$on("$routeChangeSuccess",function( $currentRoute, $previousRoute ){
        $scope.page = Views.returnView();
      });
    }]).
    
    filter('objExists', function () {
      return function (property) {
        try {
          return property;
        } catch (err) {
          return null
        }
      };
    });
    
    filters.js

    controller('AppController', ['$scope','$route','$routeParams','$location', function($scope, $route, $routeParams, $location) {    
      $scope.$on("$routeChangeSuccess",function( $currentRoute, $previousRoute ){
        render();
      });
      var render = function(){
        //Is it actually a subdomain?
        if($location.host().split(".",1).length>2){ 
          //Use subdomain folder if it is.
          var url = "views/"+$location.host().split(".",1)+"/"+$route.current.template;
          var http = new XMLHttpRequest();    
          http.onreadystatechange=function(){
            if (http.readyState==4){
              //If there isn't an overwrite, use the original.
              $scope.page = (http.status!=404)?url:("views/"+$route.current.template);
            }
          }
          http.open('HEAD', url, true);
          http.send();
        }
        else{
          //Else we are on the parent domain.
          $scope.page  = "views/"+$route.current.template;
        }
      };
    }])
    
    config(['$routeProvider', '$locationProvider', function($routeProvider, $locationProvider) {
      $locationProvider.html5Mode(true);
      $routeProvider.when('/', {
        template: 'home.html'
      });
      $routeProvider.when('/services', {
        template: 'services.html'
      });
      $routeProvider.otherwise({redirectTo: '/'});
    }]);
    
    factory('Views', function($location,$route,$routeParams,objExistsFilter) {
    
      var viewsService = {};
      var views = {
        subdomain1:{
          'home.html':'/views/subdomain1/home.html'
          },
        subdomain2:{
    
        },
        'home.html':'/views/home.html',
      };
    
      viewsService.returnView = function() {
        var y = $route.current.template;
        var x = $location.host().split(".");
        return (x.length>2)?(objExistsFilter(views[x[0]][y]))?views[x[0]][y]:views[y]:views[y];
      };
    
      viewsService.returnViews = function() {
        return views;
      };
    
      return viewsService;
    }).
    
    controller('AppController', ['$scope','Views', function($scope, Views) {    
      $scope.$on("$routeChangeSuccess",function( $currentRoute, $previousRoute ){
        $scope.page = Views.returnView();
      });
    }]).
    
    filter('objExists', function () {
      return function (property) {
        try {
          return property;
        } catch (err) {
          return null
        }
      };
    });
    

    首先要指出的是不要直接使用XMLHttpRequest,只使用angular提供的$http,同时我检查问题首先要指出的是不要直接使用XMLHttpRequest,只使用angular提供的$http,同时我检查问题