简体   繁体   中英

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.

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';
            }
        };

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM