使用多个模块路由内联视图不起作用 - $http 拦截器可能导致错误
Route with multiple modules an inline views not working - $http interceptor possibly causing errors
我正在 Angular 的项目中工作。
想法是拥有不同的模块,每个模块都可以注册自己的路由。部分将全部在同一个 HTML 内联中。但是它不起作用...
这是 plunkr http://plnkr.co/edit/n3q1FW95LD24XqTF37aZ
代码是这样的:
<body ng-class="{loaded: loaded}" ng-app="Stream" ng-controller="StreamCtrl">
<div id="wrapper" ng-show="loaded">
<div ng-view></div>
</div>
<script type="text/ng-template" id="welcome.html">
From the template
</script>
</body>
和 JS:
(function() {
"use strict";
angular.module("Default", ["ngRoute"])
.config(
["$httpProvider", "$routeProvider", "$locationProvider",
function ($httpProvider, $routeProvider, $locationProvider) {
$routeProvider
.when("/mee", {
templateUrl: "partials/welcome.html"
})
.when("/mee/index", {
templateUrl: function(params){
console.log("Getting partial url");
return "partials/welcome.html"
}
})
;
}
])
;
})();
(function() {
"use strict";
angular.module("Stream", [
"Default"
])
.config(["$locationProvider", function($locationProvider) {
}])
.run(function($rootScope, $log, $window) {
$log.info("Setting the application status as loaded!");
$rootScope.loaded = true;
});
})();
无法运行的完整代码可以在 plunkr 中看到...
终于找到答案了...
事实证明,对模板的请求(即使它们被缓存)受到 $http 拦截器的影响。
plunkr 工作在这里:
http://plnkr.co/edit/RoUhkiGP9RzCfUc6Pc3v?p=preview
这是影响数据解析的拦截器。
$httpProvider.interceptors.push(function($q) {
return {
'response': function(response) {
return response.data;
},
'responseError': function(rejection) {
return $q.reject(rejection);
}
};
});
我还需要拦截器。所以现在是这样的:
$httpProvider.interceptors.push(function($q) {
return {
'response': function(response) {
if (response.config.method == "GET" && response.config.url.split(".").pop() == "html")
return response;
return response.data;
},
'responseError': function(rejection) {
return $q.reject(rejection);
}
};
});
感谢提供模板时$http拦截问题的解决方案
我正在 Angular 的项目中工作。
想法是拥有不同的模块,每个模块都可以注册自己的路由。部分将全部在同一个 HTML 内联中。但是它不起作用...
这是 plunkr http://plnkr.co/edit/n3q1FW95LD24XqTF37aZ
代码是这样的:
<body ng-class="{loaded: loaded}" ng-app="Stream" ng-controller="StreamCtrl">
<div id="wrapper" ng-show="loaded">
<div ng-view></div>
</div>
<script type="text/ng-template" id="welcome.html">
From the template
</script>
</body>
和 JS:
(function() {
"use strict";
angular.module("Default", ["ngRoute"])
.config(
["$httpProvider", "$routeProvider", "$locationProvider",
function ($httpProvider, $routeProvider, $locationProvider) {
$routeProvider
.when("/mee", {
templateUrl: "partials/welcome.html"
})
.when("/mee/index", {
templateUrl: function(params){
console.log("Getting partial url");
return "partials/welcome.html"
}
})
;
}
])
;
})();
(function() {
"use strict";
angular.module("Stream", [
"Default"
])
.config(["$locationProvider", function($locationProvider) {
}])
.run(function($rootScope, $log, $window) {
$log.info("Setting the application status as loaded!");
$rootScope.loaded = true;
});
})();
无法运行的完整代码可以在 plunkr 中看到...
终于找到答案了...
事实证明,对模板的请求(即使它们被缓存)受到 $http 拦截器的影响。
plunkr 工作在这里:
http://plnkr.co/edit/RoUhkiGP9RzCfUc6Pc3v?p=preview
这是影响数据解析的拦截器。
$httpProvider.interceptors.push(function($q) {
return {
'response': function(response) {
return response.data;
},
'responseError': function(rejection) {
return $q.reject(rejection);
}
};
});
我还需要拦截器。所以现在是这样的:
$httpProvider.interceptors.push(function($q) {
return {
'response': function(response) {
if (response.config.method == "GET" && response.config.url.split(".").pop() == "html")
return response;
return response.data;
},
'responseError': function(rejection) {
return $q.reject(rejection);
}
};
});
感谢提供模板时$http拦截问题的解决方案