AngularJS 动态加载标签内容指令 [英] AngularJS load tabbed content directive dynamicly

查看:27
本文介绍了AngularJS 动态加载标签内容指令的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的 web 应用中有一个标签式导航,如下所示

现在我想在用户每次点击导航点之一时更改指令.我的想法是用第一个模板初始化页面.

$scope.currentDirective = $compile('

');

然后当用户单击选项卡时,我想使用新指令再次更改和编译内容.但由于某种原因,这是行不通的.为了存档这种动态内容加载,您将如何进行?我真的只想在必要时加载内容,而不仅仅是显示或隐藏它.我认为使用指令是正确的方法,但我只是坚持执行......有人有任何指针吗?(我不想使用任何 jQuery)

我尝试过的:

controller.js

 app.controller('pageController',['$scope','$compile', function($scope, $compile){var templates = ['<div first-template></div>','<div second-template></div>'];$scope.currentTemplate = $compile(templates[0]);$scope.changeTemplate = function(id) {$scope.currentTemplate = $compile(templates[id]);};}]);

HTML

<li><a ng-click="changeTemplate('1')">更改模板</a>{{当前模板}}

解决方案

UPDATE

这是一个 plunker

 app.controller('pageController',['$scope','$compile','$sce', function($scope, $compile, $sce){var templates = ['<div>first-template</div>','<div>second-template</div>'];$scope.currentTemplate = $sce.trustAsHtml(templates[0]);$scope.changeTemplate = function(id) {$scope.currentTemplate = $sce.trustAsHtml(templates[id]);};}]);

标记:

 

<button ng-click="changeTemplate('1')">更改模板</button><div ng-bind-html="currentTemplate"></div>

<小时>

对于更强大的动态内容加载,您有两个不错的选择:

如果您想再次更改和编译内容,那正是 ng-view/ ui-view 指令已经为你.

为什么不直接使用指令:

  • 您可能需要为每个标签加载不同的模板(html 部分).
  • 您可能需要根据标签更改网址(反之亦然)
  • 您可能需要为每个选项卡实例化不同的控制器.
  • ngRouteui-router 带有自己的指令.
  • 如果需要,您可以实现自己的路由模块,但这不仅仅是一个指令.

I have a tabbed navigtion in my webapp that looks like this

Now I want to Change the directive each time the user clicks on one of the Navigation points. My Idea was to init the page with the first template.

$scope.currentDirective = $compile('<div order-Sale></div>');

Then when the user clicks on a tab, I wanted to change and compile the content again with a new directive in it. But for some reason this is not working. How would you proceed in order to archive this dynamic content loading? I really want to only load the content on necessary need and not just to show or hide it. I think using directives is the right way to go for it, but I'm a but stuck at the implementation... Someone any pointer ? (I don't want to use any jQuery)

What I tried [Edit]:

The controller.js

    app.controller('pageController',['$scope','$compile', function($scope, $compile){

      var templates = ['<div first-template></div>','<div second-template></div>'];

      $scope.currentTemplate = $compile(templates[0]);

      $scope.changeTemplate = function(id) {

        $scope.currentTemplate = $compile(templates[id]);

      };

  }]);

The HTML

<div ng-controller="pageController">
    <li>
        <a ng-click="changeTemplate('1')">Change Template</a>
    </li>
    {{currentTemplate}}
</div>

解决方案

UPDATE

Here is a plunker

  app.controller('pageController',['$scope','$compile','$sce', function($scope, $compile, $sce){
    var templates = ['<div>first-template</div>','<div>second-template</div>'];
    $scope.currentTemplate = $sce.trustAsHtml(templates[0]);
    $scope.changeTemplate = function(id) {
      $scope.currentTemplate = $sce.trustAsHtml(templates[id]);
    };
  }]);

The markup:

  <div ng-controller="pageController">
    <button ng-click="changeTemplate('1')">Change Template</button>
    <div ng-bind-html="currentTemplate"></div>
  </div>


For more robust dynamic content loading you have two good alternatives:

If you want to change and compile the content again, well that's exactly what ng-view/ ui-view directives already do for you.

Why not just use a directive:

  • You probably need to load a different template (html partial) for each tab.
  • You probably need to change the url based on the tab (and vice versa)
  • You probably need to instantiate a different controller for each tab.
  • ngRoute and ui-router come with their own directives.
  • You can implement your own route module if you want but that's more than just a directive.

这篇关于AngularJS 动态加载标签内容指令的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆