简单requireJS与angularJS - 角没有定义 [英] Simple requireJS with angularJS - angular is not defined

查看:185
本文介绍了简单requireJS与angularJS - 角没有定义的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是新来requireJS,我希望得到一个简单的入门'你好worldish项目启动和运行。我失去了一些东西,但因为我得到角没有定义作为一个JS错误,当 GreetCtrl 尝试加载

I am new to requireJS, and I wanted to get a simple starter 'hello worldish' project up and running. I'm missing something though, because I get angular is not defined as a JS error when the GreetCtrl tries to load.

index.html的:

index.html:

<!DOCTYPE html>
<html ng-app="ReqApp" ng-controller="GreetCtrl">
  <body>
    <h1>{{greeting}}!</h1>
    <script src="assets/require/require.js" data-main="assets/require/main"></script>
  </body>
</html>

main.js

main.js:

require.config({
    // alias libraries paths
  paths: {
    'domReady':      'domReady',
    'angular':       '../../vendor/angular/angular.min',
    'GreetCtrl':     '../../src/app/modules/GreetCtrl',
    'app':           '../../src/app/app'
  },
  // angular does not support AMD out of the box, put it in a shim
  shim: {
    'angular': {
      exports: 'angular'
    }
  },
  // kick start application
  deps: ['./bootstrap']
});

bootstrap.js:

bootstrap.js:

define([
    'require',
    'angular',
    'app'
], function (require, ng) {
    'use strict';

    require(['domReady!'], function (document) {
        ng.bootstrap(document, ['ReqApp']);
    });
});

app.js:

app.js:

define([
  'angular',
  'GreetCtrl'
], function (ng) {
  'use strict';

  return ng.module('ReqApp', [
    'ReqApp.GreetCtrl'
  ]);
});

最后,GreetCtrl.js:

and finally, GreetCtrl.js:

angular.module( 'ReqApp.GreetCtrl', [])
.controller( 'GreetCtrl', function GreetCtrl ($scope) {
  $scope.greeting = "greetings puny human";
});

据萤火虫, GreetCtrl.js 1号线抛出的错误角度没有定义。缺少什么我在这里?

According to firebug, line 1 of GreetCtrl.js throws an error of angular is not defined. What am I missing here?

在此先感谢!

推荐答案

您还没有告诉 GreetCtrl ,它取决于因此它是未定义。试着改变 GreetCtrl 是这样

You have not told the GreetCtrl that it depends upon angular thus it is undefined. Try changing the GreetCtrl to be like this

define(['angular'], function(angular){
    angular.module( 'ReqApp.GreetCtrl', [])
    .controller( 'GreetCtrl', function GreetCtrl ($scope) {
         $scope.greeting = "greetings puny human";
    });
});

这篇关于简单requireJS与angularJS - 角没有定义的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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