Kassem
Kassem

Reputation: 8276

Change the contents of ngView dynamically

I have a requirement that says, when certain urls are requested, I need to change the view and display some uniform view with the corresponding data. This would've been easy if I could manually add routes to match those urls and then use the same templateUrl and controller to render the view.

In my case, it's a bit more complex. Those routes are being generated dynamically. Briefly, in the project I'm working on, I need to give users [developers actually] the ability to define a sitemap, and based on certain criteria the user chooses, the children of a certain node (which has a dynamically generated url) will either be displayed in the menu or as tiles in a generic view.

I've got the menu part working perfectly fine. I still need to figure out a way to feed the ngView with the proper template and data to render the tiles. Is that even possible?

UPDATE:

I think I could use the otherwise method on the $routeProvider service to achieve something like that. The documentation says that this method gets triggered when no routes have been defined for a certain URL.

I would appreciate it if someone would confirm that this is a possible solution and if there's anything better the please do share. Thanks in advance.

Upvotes: 0

Views: 97

Answers (1)

Ashwini Maddala
Ashwini Maddala

Reputation: 197

I had this problem too. Please have a look if this might help.

HTML:

<a href="" class="btn btn-success" ng-click="changeView()">Switch View</a>
<div ng-include="viewTemplate"></div>

Controller:

 $scope.changeView = function () {
            if ($scope.isParcelYearGrid) {
                $scope.viewTemplate = '../app/parcelMassEntry/parcelMassEntryGrid.html';
            }
            else {
                $scope.viewTemplate = '../app/parcelMassEntry/parcelMassEntry.html';
            }
        };

Upvotes: 1

Related Questions