ajax - 带有 Angular : loading tab content on click only with $http 的选项卡

标签 ajax angularjs tabs angular-ui

我有包含大量数据的大表格,
所以我想要每个标签都有大量数据的标签。

我希望在单击选项卡标题时延迟加载选项卡内容,然后在以后再次选择时不需要再次重新加载。

我认为这个例子符合我想要的方向:
angular-ui tabs loading template in tab-content

但这似乎加载了一个静态模板:

<tabs>
    <pane active="pane.active"
          heading="{{pane.title}}"
          ng-repeat="pane in panes">
        <div ng-include="pane.content"></div>
    </pane>
</tabs>

如何使用 $http.get() 动态加载 Pane 的内容?
注意:这已经是通过 ng-view 路由加载的页面,所以我不能做嵌套路由。

编辑:每个标签的内容都大不相同,所以理想情况下,我会为每个标签或类似的东西提供一个功能和模板......

我猜 angular-ui 是解决这个问题的好方法?

最佳答案

我自己很好奇如何通过 ajax 加载标签。这是我制定的一个小演示。

标签有一个 select选择时触发的属性。所以我使用以下标签:

<tab heading="{{tabs[0].title}}" select="getContent(0)">
       <div ng-hide="!tabs[0].isLoaded">
        <h1>Content 1</h1>
          <div ng-repeat="item in tabs[0].content">
            {{item}}
          </div>
      </div>
      <div  ng-hide="tabs[0].isLoaded"><h3>Loading...</h3></div>
   </tab>

Controller :
 $scope.tabs = [
    { title:"AJAX Tab 1", content:[] , isLoaded:false , active:true},
    {  title:"Another AJAX tab", content:[] , isLoaded:false }
  ];


  $scope.getContent=function(tabIndex){

    /* see if we have data already */
    if($scope.tabs[tabIndex].isLoaded){
      return
    }
    /* or make request for data , delayed to show Loading... vs cache */
    $timeout(function(){
        var jsonFile='data'+(tabIndex +1)+'.json'
        $http.get(jsonFile).then(function(res){
            $scope.tabs[tabIndex].content=res.data;
            $scope.tabs[tabIndex].isLoaded=true;
        });

    }, 2000)

  }

将缓存移动到服务,因此如果用户切换 View 并返回,数据仍将在服务缓存中

DEMO

关于ajax - 带有 Angular : loading tab content on click only with $http 的选项卡,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19781004/

相关文章:

javascript - jquery ajax返回HTML元素

javascript - jQuery AJAX 调用触发错误函数

javascript - 如何从 HTTP.call "GET"返回 Meteor.JS 中的值

vim - 是否可以在 VIM 中使用单独的缓冲区创建 2 个选项卡?

tabs - Visual Studio 2013 正在插入制表符而不是空格

php - 单击图像后从数据库中获取值

javascript - 将 AngularJs Widget 集成到 Adob​​e Captivate 中

angularjs - 在全新的 ubuntu 安装中使用 NPM 安装 angular-cli 时出错

javascript - 覆盖 $templateCache 使其不区分大小写

Android:选项卡中的选项卡