关于angular通过require调用第三方插件的问题

参考文献列表

问题背景

通过requirejs使用angular来开发,需要引用第三方的一个弹窗插件,如ui-notification。见文章http://www.jianshu.com/p/cfc7e39cbbb5,那是通过webpack来使用的。
如果不引用第三方插件,只是angular写法,也没有问题。
html代码

<html lang="zh" ng-app="app"  >

js写法

require.config({
    paths:{
        'angular':"./common/angular",
        'ui-notification':"./common/angular-ui-notification.min",
        'config':"./config",
        'getUrl':"./getUrl",
        'util':"./util",
        'setLang':"./setLang",
        'angular-route':"./common/angular-route",
        'appstart':"./appstart"
    },
    shim: {
        'angular': {
            exports: 'angular'
        },
        'ui-notification':{
            deps:['angular'],
            exports:'ui-notification'
        },
        'angular-route': { deps: ['angular'] }

    },
})
require(['angular'],function (angular) {
    angular.module("app", []);
    angular.module("app").controller('loginController', ['$scope',, function($scope) {
        $scope.greeting = 'Hello, world!';
        console.log(1)
    }]);

})


现在引入ui-notification,一直报错。写法如下。

require(['angular','ui-notification'],function (angular) {
    angular.module("app", ['ui-notification']);
    angular.module("app").controller('loginController', ['$scope','Notification', function($scope,Notification) {
        Notification("hello")
        $scope.greeting = 'Hello, world!';
        console.log(1)
    }]);

})

一直报下面的错误:

angular.js:78 Uncaught Error: [$injector:modulerr] Failed to instantiate module app due to:
Error: [$injector:nomod] Module 'app' is not available! You either misspelled the module name or forgot to load it. If registering a module ensure that you specify the dependencies as the second argument.

一直一脸懵逼中。
后来找到这篇文章,
https://www.sitepoint.com/using-requirejs-angularjs-applications/

The Angular application cannot be bootstrapped using the ng-app directive as the required script files are loaded asynchronously. The right approach here is to use manual bootstrapping. This has to be done in a special file called main.js. This needs the file defining the Angular module to be loaded first. The code for this file is shown below.

大致意思是:当angular 所依赖的js文件异步加载时,它用ng-app指令是无法启动angular程序的。正确的方法是手动启动use manual bootstrapping。这不得不写在一个特殊的文件main.js。这需要先加载已定义angular module的文件。代码写法如下。

require(['app/ideasModule'],
  function() {
    angular.bootstrap(document, ['ideasApp']);
  }
);

这里的ideasModule文件写的就是模块

回到我们自己的文件写法。

RequireJS 会自动加载脚本 scripts/main.js, 在 main.js 文件里面进行配置, 来动态加载 AngularJS , 文件内容以及说明如下:

requirejs.config({
    // 所有脚本的跟目录, 相对于 html
    baseUrl: 'scripts',
    paths: {
        // angular 脚本的路径, 相对于 baseUrl
        'angular': 'lib/angular/angular',
        'angular-route': 'lib/angular/angular-route'
    },
    shim: {
        // 需要导出一个名称为 angular 的全局变量, 否则无法使用
        'angular' : { exports: 'angular' },
        // 设置 angular 的其它模块依赖 angular 核心模块
        'angular-route': { deps: ['angular'] }
    }
});

完整的配置请看这里: [RequireJS Shim for AngularJS 1.3.0] //根本打不开(https://gist.github.com/beginor/e57e596be4040c404044)
有了上面的配置之后, 在文件的结尾添加下面的测试:

说明:这时候根本是加载不出来angular-route的。会报错的。

下面写正确的思路。

首先新建一个main.js文件,作为主入口

require.config({
    paths:{
        'angular':"./common/angular",
        'ui-notification':"./common/angular-ui-notification.min",
     },
    shim: {
        'angular': {
            exports: 'angular'
        },
        'ui-notification':{
            deps:['angular'],
            exports:'ui-notification'
        },
    },
})
//上面是配置
//下面是手动启动app,但是启动前先加载app1.js文件。
//如果有多个controller文件,则可以依次放在数组中
require(['app1'],function () {
    angular.bootstrap(document, ['app']);
})

再新建app1.js文件
注意:

define("app1",XXXX)中的app1必须和文件名一致(除了.js),否则一直会提示

angular is not defined
define('app1', ['angular','ui-notification'], function(angular) {
    // 使用严格模式
    'use strict';
    // 定义 angular 模块
    var app = angular.module('app', ['ui-notification']);
    // 定义 DemoController , 只定义一个属性 greeting 给界面绑定。
    app.controller('loginController', ['$scope','Notification', function($scope,Notification) {
        Notification("1hello")
        $scope.greeting = 'Hello, world!';
    }]);
    return app;
});

注意:bootstrap处的app必须与app1中的module的app一致。

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 216,372评论 6 498
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 92,368评论 3 392
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 162,415评论 0 353
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 58,157评论 1 292
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 67,171评论 6 388
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 51,125评论 1 297
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 40,028评论 3 417
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 38,887评论 0 274
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 45,310评论 1 310
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 37,533评论 2 332
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 39,690评论 1 348
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 35,411评论 5 343
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 41,004评论 3 325
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 31,659评论 0 22
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 32,812评论 1 268
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 47,693评论 2 368
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 44,577评论 2 353

推荐阅读更多精彩内容

  • 大部分的后端会很很鄙视前端。我也不知道为什么,可能大部分人都会觉得脚本语言根本不算语言。 大多人 会叫我们切图仔,...
    小黑的眼阅读 3,330评论 0 15
  • 晚安,明天要早起的;今天忙碌了一天脑袋都觉得很累很累了;这些天天气原因也注定玩不了滑板;呵呵;我是无滑板不人生的主...
    蜕变ING阅读 215评论 0 0
  • 是没有办法接受现在这个生活 只自苦自卑的从流浪中寻找一丝安稳 从朝阳东升到皓月当空 努力的让每天不至于太过残...
    随我北来阅读 244评论 2 2
  • 杏月惊蛰到,虫蛹茧出壳。 虫鸟喜风月,抖姿迎春笑。
    杨柳依依簡阅读 171评论 0 3