仅在需要 AngularJS 时才注入模块依赖项(如插件) [英] Inject module dependency (like plugin) only if needed AngularJS

查看:26
本文介绍了仅在需要 AngularJS 时才注入模块依赖项(如插件)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有 2 个页面(我不使用 angularrouting - 这个约束).

在其中一个我想使用指令 ui-grid就像在这个演示中:

var app = angular.module('myApp', ['ui.grid']);app.controller('mainCtrl', function($scope) {$scope.myData = [{"firstName": "考克斯","lastName": "卡尼","company": "Enormo",就业":真的},{"firstName": "洛林","lastName": "明智的","company": "运输机",受雇":假},{"firstName": "南希","lastName": "水域","company": "富尔顿",受雇":假}];});

<script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.2.26/angular.js"></script><script src="http://ui-grid.info/release/ui-grid-unstable.js"></script><link rel="stylesheet" href="http://ui-grid.info/release/ui-grid-unstable.css" type="text/css"><div ng-app="myApp"><div ng-controller="mainCtrl"><div id="grid1" ui-grid="{ data: myData }" class="grid"></div>

我的问题是,是否有办法在任何情况下都不将 ui-grid 依赖项注入应用程序,但仅在我需要时才注入.

类似于:

app.controller('mainCtrl', function($scope) {app.$inject('ui-grid');});

更新

我尝试这样做:

var ui_grid = $injector.get('ui-grid');

但是我有一个错误:

<块引用>

未知提供者:ui-gridProvider <- ui-grid

http://errors.angularjs.org/1.2.26/$injector/unpr?p0=ui-gridProvider%20%3C-%20ui-grid

解决方案

Core Angular API 不提供此功能.ocLazyLoad 是 Angular 中用于延迟加载模块和组件的流行库.

您可以在他们的页面上找到更多信息:https://oclazyload.readme.io

或 GitHub 存储库:https://github.com/ocombe/ocLazyLoad

I have 2 pages (I don't use the angular's routing - This constraint).

In one of them I want to use the directive ui-grid like in this demo:

var app = angular.module('myApp', ['ui.grid']);
app.controller('mainCtrl', function($scope) {
  $scope.myData = [
    {
        "firstName": "Cox",
        "lastName": "Carney",
        "company": "Enormo",
        "employed": true
    },
    {
        "firstName": "Lorraine",
        "lastName": "Wise",
        "company": "Comveyer",
        "employed": false
    },
    {
        "firstName": "Nancy",
        "lastName": "Waters",
        "company": "Fuelton",
        "employed": false
    }
  ];
});

<script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.2.26/angular.js"></script>
<script src="http://ui-grid.info/release/ui-grid-unstable.js"></script>
<link rel="stylesheet" href="http://ui-grid.info/release/ui-grid-unstable.css" type="text/css">

<div ng-app="myApp">
  <div ng-controller="mainCtrl">
    <div id="grid1" ui-grid="{ data: myData }" class="grid"></div>
  </div>
</div>

My question is if there is a way to not inject the ui-grid dependency to the app in any case but only when I need it.

Something like:

app.controller('mainCtrl', function($scope) {
   app.$inject('ui-grid');
});

Update

I tried to do:

var ui_grid = $injector.get('ui-grid');

But I've got an error:

Unknown provider: ui-gridProvider <- ui-grid

http://errors.angularjs.org/1.2.26/$injector/unpr?p0=ui-gridProvider%20%3C-%20ui-grid

解决方案

Core Angular API does not provide this feature. ocLazyLoad is the popular library for lazy loading of modules and components in Angular.

You can find more info on their page: https://oclazyload.readme.io

or the GitHub repository: https://github.com/ocombe/ocLazyLoad

这篇关于仅在需要 AngularJS 时才注入模块依赖项(如插件)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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