0

Is there any way for me to change a view's controller in angular?

angular.module('app', [
  'ngRoute'
]).
config(['$routeProvider', function($routeProvider) {
  $routeProvider.when('/', {
    templateUrl: 'views/home/index.html',
    controller: 'home',
    replace: false
  })
  $routeProvider.otherwise({redirectTo: '/'});
}]);

What I'm trying to do is give another controller for index.html in runtime.

Bomin
  • 1,619
  • 5
  • 24
  • 39

2 Answers2

1

Instead of messing with the view, define a couple of views, and change the current configuration with state.

ex: view1 has controller#1 and url#1 view2 has controller#2 and url#1

Read this blog for a more thorough explanation: http://txt.fliglio.com/2013/05/angularjs-state-management-with-ui-router/

  • Thanks, but the problem is I need to let all the views to share the same path. Can I do that? – Bomin Apr 15 '15 at 08:28
0

angular.module('myapp', []).config(function($provide, $routeProvider) { $provide.factory('$routeProvider', function () { return $routeProvider; }); }).run(function($routeProvider, $http) { $routeProvider.when('/', { templateUrl: 'views/main.html', controller: 'MainCtrl' }).otherwise({ redirectTo: '/' }); $http.get('/dynamic-routes.json').success(function(data) { $routeProvider.when('/', { templateUrl: 'views/main.html', controller: 'MainCtrl' }); // you might need to call $route.reload() if the route changed $route.reload(); }); });

Community
  • 1
  • 1
Smrutiranjan Sahu
  • 6,911
  • 2
  • 15
  • 12