如何使用routeprovider对网址发帖请求?提供以下示例代码
<script>
var app = angular.module("myApp", ["ngRoute"]);
app.config(function($routeProvider) {
$routeProvider
.when("/", {
templateUrl : "main.htm"
})
.when("/red", {
templateUrl : "red.htm"
})
.when("/green", {
templateUrl : "green.htm"
})
.when("/blue", {
templateUrl : "blue.htm"
});
});
</script>
答案 0 :(得分:2)
您可以使用resolve
:
.when("/", {
templateUrl : "main.htm",
resolve: {
data: function($http) {
$http.post('/yourUrl', yourData)
.then(function(res) {
return res;
}, function(err) {
console.log(err);
return null;
})
}
}
})
然后在你的控制器中,
.controller(function(data) {
console.log(data);
})
注意:这本身不使用 routeProvider ,因为进行REST调用不是routeProvider的用途。 Angular只能通过$http
服务来实现。我假设您只想在路由定义中进行REST调用。
Protip 更好的方法是在模块中定义服务,并在路由解析中插入模块,而不是注入{{1}直接我这样做只是为了简洁