【问题标题】:Angular JS: Load CSS and JS files dynamicallyAngular JS:动态加载 CSS 和 JS 文件
【发布时间】:2013-07-17 08:45:19
【问题描述】:

我正在开发一个网络应用程序。我正在使用 AngularJS 将所​​有文件动态加载到 UI 中。

我有一个index.html 文件,所有文件都将在点击或加载时动态加载。

//index.html
<signin button> <signup button> // etc.,
//on-clicking signin button a SignIn-page will load in the below div

<div id="bodyview"></div>
// end of index.html

现在我的登录页面看起来有点像下面的结构

    /*a div where my accordion menu will load when the 
     *below submit button is clicked*/

    <div id="menu"></div>

    //other sign-in-form stuff will follow on...

    //submit button at the end
    <submit button> //only on clicking the submit button the menu will be loaded

现在我的问题是,虽然我能够加载手风琴 menu.html 文件,但我无法加载它所依赖的 css 和 js 文件。我研究过 stackoverflow,但没有一个对我有用。

任何人都可以帮助确定使用 AngularJS 加载 css 和 js 依赖项的方法

【问题讨论】:

    标签: angularjs angularjs-directive angularjs-scope angular-ui


    【解决方案1】:

    只需编写一个服务,将 CSS 和 JS 文件添加到&lt;head&gt;

    这是一个用于动态加载 CSS 文件的示例服务。您可以轻松修改它以加载 JS 文件。

    https://github.com/Yappli/angular-css-injector

    【讨论】:

    • 这个也可以用来注入JS文件吗?
    • 我们如何使用它添加js文件?例如控制器。
    • 如果我这样做,视图会在加载 CSS 之前显示...(并且您链接的服务已弃用)
    【解决方案2】:

    目前我使用 angular-css:https://github.com/door3/angular-css 恕我直言,这是目前最好、最灵活的解决方案之一。

    【讨论】:

      【解决方案3】:

      我没有使用 AngularJS 的方式,而是尝试使用 JQuery 加载文件。它对我有用。 You can check this link for reference

      【讨论】:

      • 在这里我想我不会看到“为什么不使用 jQuery?”的答案。路线...
      • OP 正在尝试使用 AngularJS 加载文件。添加像 JQuery 这样的库只是为了加载文件并不是一个好的解决方案。这也可以在纯 javascript 中完成,如 here 所述
      • AngularJS 包含 jqLit​​e,因此可能不需要额外的依赖:docs.angularjs.org/api/ng/function/…
      【解决方案4】:

      我使用 jquery 来完成这样的工作

      /** resolve will come inside your route .when() function **/
      resolve: {
              theme: function ($route, $q) {
                  changeCss($route, $q);
              }
          }
      
      /** This will come just outside your route .when() function, make sure the following function shall be in scope of the route provider **/
          var changeCss = function ($route, $q) {
              var defer = $q.defer();
              // Change the CSS as per the param received
              if ($route.current.params.css != undefined) {
                  if ($route.current.params.css === ‘dark’) {
                      loadCSS(“assets / css / dark.css”);
                      defer.resolve();
                  }
                  if ($route.current.params.css === ‘light’) {
                      loadCSS(“assets / css / light.css”);
                      defer.resolve();
                  } else {
                      defer.resolve();
                  }
              }
              return defer.promise;
          }
          var loadCSS = function (href) {
              var cssLink = $(“ < link rel = ’stylesheet’ type = ’text / css’ href = ’”+href + “‘ > ”);
              $(“head”).append(cssLink);
          };
      

      我使用了 Promise,因为这将确保在显示/加载 angularjs 路由/页面之前加载 css。

      【讨论】:

        【解决方案5】:
        app.controller('MyCtrl', function($scope,$compile) {
            $("#my").append( $compile("<script src='my.js'>")($scope) );
        });
        

        【讨论】:

        • 您不应该在控制器中进行 DOM 操作。这是使用角度时的陷阱之一
        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2017-05-07
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多