angular.js - 已实现的angularjs项目用requirejs进行模块化时遇到问题
问题描述
其实就是对todoMVC项目用requirejs进行模块化。原本的angularjs是分别在controller、directive、service中分别定义了一个模块来代表这三者。下面是directive:todoFocus.js
(function () { ’use strict’ angular.module(’todoFocus’,[]).directive(’todoFocus’,function ($timeout){return function (scope,element,attrs){ scope.$watch(attrs.todoFocus,function (newVal){if(newVal){ $timeout(function(){element[0].focus(); },0,false);} })} })})()
上面就是一个directive。之后在app.js中
(function () { ’use strict’; angular.module(’todomvc’, [’todoCtrl’, ’todoFocus’, ’todoStorage’]);})();
我用requirejs模块化之后directive变成了这样:
(function () { ’use strict’ define([’angular’],function (angular) {angular.module(’todoFocus’,[]).directive(’todoFocus’,function ($timeout){return function (scope,element,attrs){ scope.$watch(attrs.todoFocus,function (newVal){if(newVal){ $timeout(function(){element[0].focus(); },0,false);} })} })return ’todoFocus’; })})()
然后app.js变成了这样:
(function () { ’use strict’; require([’angular’],function (angular) {require([ ’controllers/todoCtrl’, ’directives/todoFocus’, ’services/todoStorage’ ],function (todoCtrl,todoFocus,todoStorage) {angular.module(’todomvc’,[todoCtrl,todoFocus,todoStorage]);angular.bootstrap(document, [’todomvc’]); }) })})();
之后打开网页发现所有的js文件都加载出来了,但是并不能实现效果。。

是不是app.js不能这么写。没怎么用过requireJS/(ㄒoㄒ)/~~
贴一下我的文件路径
下面是我的main.js
(function (win) { ’use strict’; require.config({paths: { angular: ’../node_modules/angular/angular’},shim: { //专门用来配置不兼容的模块 angular: { exports: ’angular’ //输出变量名,表示这个模块外部调用时的名称 }},deps: [’app’] //deps数组,表示该模块依赖app模块,所以要先加载app模块});})(window)
感觉我的路径没啥问题呀/(ㄒoㄒ)/~~
问题解答
回答1:模块依赖注入错误了,检查下引用路径
相关文章:
1. html5 - javascript写业务有用到什么编程范式没?2. 用CSS3 box-sizing 属性实现两个并排的容器,如果想让容器中间有间隔该如何实现3. javascript - vue 手机端项目在进入主页后 在进入子页面,直接按返回出现空白情况4. javascript - jQuery post()方法,里面的请求串可以转换为GBK编码么?可以的话怎样转换?5. javascript - nodejs调用qiniu的第三方资源抓取,返回401 bad token,为什么6. html5 - vue-cli 装好了 新建项目的好了,找不到项目是怎么回事?7. python3.x - python 中的maketrans在utf-8文件中该怎么使用8. javascript - immutable配合react提升性能?9. javascript - 一排三个框,各个框的间距是15px,距离外面的白框间距也是15px,这个css怎么写?10. mysql - C#连接数据库时一直这一句出问题int i = cmd.ExecuteNonQuery();

网公网安备