我在/tests中有一个测试表,我希望能够通过访问/tests/:id/hide永久地隐藏特定的测试。我有一个函数这样做,我只需要想出一种方法来调用它,而不必调用一个新的控制器。在这样做时,我还想重定向回/tests。
angular.module('WebApp.services', []).
factory('riakAPIService', function($http) {
var riakAPI = {};
riakAPI.hideTest = function(key) {
return $http({
// Some code for setting a "hide" flag for this test in the database
});
}
});当用户转到riakAPI.hideTest(id)时,有没有一种很好的调用/tests/:id/hide的方法?
angular.module('WebApp', ['WebApp.controllers','WebApp.services','ngRoute']).
config(['$routeProvider', '$locationProvider', function($routeProvider, $locationProvider) {
$routeProvider.
when("/tests", {templateUrl: "partials/tests.html", controller: "testsController"}).
when("/tests/:id", {templateUrl: "partials/test.html", controller: "testController"}).
otherwise({redirectTo: '/tests'});
}]);发布于 2014-12-04 22:24:10
我认为最好的方法是在这里使用解决方案。
$routeProvider.when('/tests/:id',{
templateUrl : 'partials/tests.html',
controller : 'testController',
resolve : {
hidden : function(riakAPIService){
return riakAPIService.hideTest();
}
}
})为了服务
angular.module('WebApp.services', []).
factory('riakAPIService', function($http,$location) {
var riakAPI = {};
riakAPI.hideTest = function(key) {
return $http({
// Some code for setting a "hide" flag for this test in the database
}).then(function(){
$location.path('/tests');
});
}
});https://stackoverflow.com/questions/27305084
复制相似问题