0

Is there a way to have the same controller, but exposes different scope methods/variable for different routes?

I am currently using ui-router and would like to expose different methods/variables for /new, /edit, /delete

3 Answers 3

3

Doesn't it make more sense to have the same template but with different controllers?

Sign up to request clarification or add additional context in comments.

1 Comment

I would agree with you on that, having the same controller behaving differently i believe will violate Open-close principle for sure.
1

Yes.

Using ui-router,

angular.module("myModule")
.controller("myController",function($scope,$stateParams){
    if ($stateParams.mySubView === "/new") {
       $scope.hello = function(){alert("Hello")};
    } else {
       $scope.hello = function(){alert("Hi")};
    }
})

Comments

0

You can have all routes pointing to the same controller, but in the controller use the $stateParams to determine which method it needs to invoke. Don't think it needs to be any harder than that? Unless I've misunderstood the question.

1 Comment

Yes, that'd work perfectly. I was wondering if there is a way to move more of the logic into the $stateProvider where I can just specify a method that would invoke a function with all the variables and $scope methods

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.