angular.js - 已實現(xiàn)的angularjs項目用requirejs進(jìn)行模塊化時遇到問題
問題描述
其實就是對todoMVC項目用requirejs進(jìn)行模塊化。原本的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’]); }) })})();
之后打開網(wǎng)頁發(fā)現(xiàn)所有的js文件都加載出來了,但是并不能實現(xiàn)效果。。
是不是app.js不能這么寫。沒怎么用過requireJS/(ㄒoㄒ)/~~
貼一下我的文件路徑
下面是我的main.js
(function (win) { ’use strict’; require.config({paths: { angular: ’../node_modules/angular/angular’},shim: { //專門用來配置不兼容的模塊 angular: { exports: ’angular’ //輸出變量名,表示這個模塊外部調(diào)用時的名稱 }},deps: [’app’] //deps數(shù)組,表示該模塊依賴app模塊,所以要先加載app模塊});})(window)
感覺我的路徑?jīng)]啥問題呀/(ㄒoㄒ)/~~
問題解答
回答1:模塊依賴注入錯誤了,檢查下引用路徑
相關(guān)文章:
1. 在mybatis使用mysql的ON DUPLICATE KEY UPDATE語法實現(xiàn)存在即更新應(yīng)該使用哪個標(biāo)簽?2. mysql - 怎么生成這個sql表?3. mysql儲存json錯誤4. 哭遼 求大佬解答 控制器的join方法怎么轉(zhuǎn)模型方法5. mysql - 表名稱前綴到底有啥用?6. Navicat for mysql 中以json格式儲存的數(shù)據(jù)存在大量反斜杠,如何去除?7. 編輯成功不顯示彈窗8. 怎么php怎么通過數(shù)組顯示sql查詢結(jié)果呢,查詢結(jié)果有多條,如圖。9. mysql - 數(shù)據(jù)庫表中,兩個表互為外鍵參考如何解決10. sql語句 - 如何在mysql中批量添加用戶?
