Divs
Divs

Reputation: 1618

Load different angular controller at click of a button

I am working on a Single Page Application. Page A , Page B are partials. We have a button "btnA" on "Page A" rendered using "controllerA". At click of "btnA" we need to perform "serviceA.somework()" and at successful completion, load "Page B" using "controllerB" with some params.

Is following the best way to achieve this?

within controllerA.onClickBtnA() { $location.path(PageB).search({param:'value'})}

Upvotes: 1

Views: 46

Answers (1)

Joy
Joy

Reputation: 9560

$routeProvider is the best choice for your scenario. You should not load the controller manually in any way. The Angular router does the job for you.

A simple example from the angular official API page: http://plnkr.co/edit/foLnNL7koXzUYavnYFFw?p=preview.


Update

Your proposal is basically doing what $routeProvider does. $routeProvider provides:

  1. RegExp in path definition
  2. Browsing history
  3. A resolve function to control access to current path
  4. Place to define your controller and template for each path
  5. Mechanism to deal with path parameters
  6. ...

And it is well tested and supported. You'd better have a try.

Upvotes: 1

Related Questions