使用 Zurb Foundation 与 AngularJS 交换

2024-01-08

我正在开发一个 AngularJS 项目,该项目使用 Zurb Foundation 作为其 CSS 框架。我正在尝试弄清楚如何使用基金会的数据交换 http://foundation.zurb.com/docs/components/interchange.html在 AngularJS 视图的上下文中。这可能吗,我似乎无法让它发挥作用。这就是我目前所拥有的:

索引.html

<!DOCTYPE html>
<html ng-app='app' xmlns="http://www.w3.org/1999/xhtml">
<head>
    <title>Test</title>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">

    <link rel="stylesheet" href="foundation/normalize.css" />
    <link rel="stylesheet" href="foundation/foundation.min.css" />
    <link rel="stylesheet" href="app.css" />
</head>
<body>
    <div id="view" ng-view></div>

    <script type="text/javascript" src="jquery/2.0.3/jquery.min.js"></script>
    <script type="text/javascript" src="foundation/foundation.min.js"></script>

    <script type="text/javascript" src="angularjs/1.2.7/angular.min.js"></script>
    <script type="text/javascript" src="angularjs/1.2.7/angular-route.min.js"></script>

    <script type="text/javascript" src="app.js"></script>
</body>
</html>

app.js

angular.module('app', ['ngRoute'])
  .config(function ($routeProvider, $locationProvider) {
    $locationProvider.html5Mode(true);

    $routeProvider
      .otherwise({ templateUrl: '/views/interchange.html', controller: myCtrl });
    })
    .run(function ($rootScope, $location) {
      $rootScope.$on('$viewContentLoaded', function () {
        $(document).foundation();
      });
    })
;

function myCtrl() {
    console.log('loading controller');
}

交换.html

<article>
    testing interchange
    <div data-interchange="[phone.html, (small)], [portrait.html, (medium)], [landscape.html, (large)]"></div>
</article>

当我加载应用程序时,我可以看到“测试交换”。但是,基于屏幕尺寸的内容(phone.html、portrait.html、landscape.html)不会显示。 Phone.html、Portrait.html 和landscape.html 仅在DIV 元素内包含有效表示“Phone”、“Portrait”和“Landscape”的文本。

有没有办法在 AngularJS ng-view 中利用 Foundation 的数据交换内容?如果是这样,怎么办?谢谢


这里的要点是 Zurb 的数据交换功能在 DOMContentLoaded 事件上运行,并且 AngularJS 视图的加载是异步的。所以,事件已经发生了。

为了解决这个问题,您需要使用 $(document).foundation('interchange', 'reflow'); 手动触发数据交换。或 $(document).foundation('reflow');回流所有组件。

为了在正确的地方触发这个,你需要监听 AngularJS 路由系统上一个名为 $routeChangeSuccess 的特殊事件。像这样的东西:

module.run(function ($rootScope) {
    $rootScope.$on('$routeChangeSuccess', function () {
        $(document).foundation('reflow');
    });
});

希望这对您有帮助。

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

使用 Zurb Foundation 与 AngularJS 交换 的相关文章

随机推荐