1
我正在嘗試使用由RequireJS提供的Angular應用程序,但由於我不斷收到Argument '(contoller)' is not a function, got undefined
的錯誤,因此無法分割控制器文件。任何想法我做錯了什麼?由Requirejs提供的角度應用程序 - 參數'(控制器)'不是函數,沒有定義?
應用程序/ main.js,
require.config({
baseUrl: "",
// alias libraries paths. Must set 'angular'
paths: {
'domReady': 'bower_components/requirejs-domready/domReady',
'angular': 'bower_components/angular/angular',
'angular-route': 'bower_components/angular-route/angular-route',
'jquery': 'bower_components/jquery/dist/jquery'
},
// Add angular modules that does not support AMD out of the box, put it in a shim
shim: {
'angular': {
exports: 'angular'
},
'angular-route': {
exports: 'angular'
}
},
// kick start application
deps: ['app/bootstrap']
});
應用程序/ app.js,
define([
'angular',
'angular-route',
'jquery'
], function (ng,ngRoute,$) {
'use strict';
console.log($('h1').length);
return ng.module('app', ['ngRoute']).
config(['$routeProvider', function($routeProvider) {
$routeProvider.when('/home', {
templateUrl: 'view/home.html',
controller: 'HomeCtrl',
controllerUrl: 'app/home'
});
$routeProvider.when('/view1', {
templateUrl: 'view/view1.html',
controller: 'View1Ctrl',
controllerUrl: 'app/view'
});
//$routeProvider.otherwise({redirectTo: '/home'});
}]);
});
應用程序/ home.js,
define(['app/app'], function (app) {
app.controller('HomeCtrl', function ($scope) {
$scope.message = "Message from HomeCtrl";
});
});
誤差,
Error: [ng:areq] Argument 'HomeCtrl' is not a function, got undefined
但如果我Concat的該控制器ng.module('app', ['ngRoute']).config(...)
那麼他們正常工作後,
應用程序/ app.js,
define([
'angular',
'angular-route',
'jquery'
], function (ng,ngRoute,$) {
'use strict';
console.log($('h1').length);
return ng.module('app', ['ngRoute']).
config(['$routeProvider', function($routeProvider) {
$routeProvider.when('/home', {
templateUrl: 'view/home.html',
controller: 'HomeCtrl',
//controllerUrl: 'app/home'
});
$routeProvider.when('/view1', {
templateUrl: 'view/view1.html',
controller: 'View1Ctrl',
//controllerUrl: 'app/view'
});
//$routeProvider.otherwise({redirectTo: '/home'});
}]).controller('HomeCtrl', function ($scope) {
$scope.message = "Message from HomeCtrl";
}).controller('View1Ctrl', function ($scope) {
$scope.message = "Message from View1Ctrl";
});
});
爲什麼?
謝謝。我怎麼做到這一點 - '你可以創建兩個RequireJS模塊(定義),一個用於home.js,一個用於app.js.然後將主模塊作爲依賴項添加到應用程序模塊,以便它們按正確的順序加載。 – laukok 2014-10-28 10:12:55
'app.js'和'home.js'已經在其中定義(...)'... – laukok 2014-10-28 10:14:23
@tealou我添加了一些說明 – metacubed 2014-10-28 10:15:15