0

I have a table of tests in /tests, and I want to be able to permanently hide a specific test by going to /tests/:id/hide. I have a function that does this, I just need to figure out a way to call it without having to invoke a new controller. When doing this, I also want to redirect back to /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
            });
        }
    });

Is there a pretty way to call riakAPI.hideTest(id) when the user goes to /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'});
    }]);

1 Answer 1

1

I think the best approach is to use the resolve param here .

$routeProvider.when('/tests/:id',{
    templateUrl : 'partials/tests.html',
    controller : 'testController',
    resolve : {
        hidden : function(riakAPIService){
              return riakAPIService.hideTest();
        }
    }
})

And for the service

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');
            });
        }
    });
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.