其實(shí)就是對(duì)todoMVC專案用requirejs進(jìn)行模組化。原本的angularjs分別在controller、directive、service中分別定義了一個(gè)模組來(lái)代表這三者。
下面是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);
}
})
}
})
})()
上面就是一個(gè)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']);
})
})
})();
之後打開(kāi)網(wǎng)頁(yè)發(fā)現(xiàn)所有的js檔案都載入出來(lái)了,但是並不能實(shí)現(xiàn)效果。 。
是不是app.js不能這麼寫。沒(méi)怎麼用過(guò)requireJS/(ㄒoㄒ)/~~
貼一下我的檔案路徑
下面是我的main.js
(function (win) {
'use strict';
require.config({
paths: {
angular: '../node_modules/angular/angular'
},
shim: { //專門用來(lái)配置不兼容的模塊
angular: {
exports: 'angular' //輸出變量名,表示這個(gè)模塊外部調(diào)用時(shí)的名稱
}
},
deps: ['app'] //deps數(shù)組,表示該模塊依賴app模塊,所以要先加載app模塊
});
})(window)
感覺(jué)我的路徑?jīng)]啥問(wèn)題呀/(ㄒoㄒ)/~~