Dynamically Loading Controllers and ng-include

前端 未结 3 502
夕颜
夕颜 2021-01-31 16:57

At the moment I have an app that has a sidebar, and the sidebar loads different html templates using ng-include based on what operation the user chooses to do. It\'

3条回答
  •  挽巷
    挽巷 (楼主)
    2021-01-31 17:25

    I like this data driven scenario, just manipulate the rows in the templates:

    $scope.templates = [
        { name: 'include1.html', url: 'partials/include1.html' }
    ];
    $scope.addTemplate = function(name, url) {
        $scope.templates.push({ name: name, url: url });
    };
    

    and the markup:

    To add or remove views, just modify the templates et voila! If you need more controller code, then you can include a link to the script in the include. I guess there may be complications with binding to data in the partial view itself, but $compile should resolve those.

提交回复
热议问题