gpt4 book ai didi

angularjs - 通过 AngularAMD 将 AngularJS 与 RequireJS 结合使用 : Cannot read property 'directive' of undefined

转载 作者:行者123 更新时间:2023-12-03 06:59:04 24 4
gpt4 key购买 nike

创建了一个非常基本的原型(prototype) AngularJS 项目后,我想将其迁移为使用 RequireJS 来加载模块。我根据 AngularAMD 修改了我的应用程序和 AngularAMD-sample项目。

现在,当我访问默认路由时,我得到:

Uncaught TypeError: Cannot read property 'directive' of undefined

我一直在摸不着头脑,为什么对“app”的依赖没有得到满足。如果有人能发现我明显做错了什么,我将不胜感激。

我已经把我的项目的源代码here on GitHub ,但关键部分如下:

ma​​in.js

require.config({

baseUrl: "js/",

// alias libraries paths
paths: {
'angular': '../bower_components/angular/angular',
'angular-route': '../bower_components/angular-route/angular-route',
'angular-resource': '../bower_components/angular-resource/angular-resource',
'angularAMD': '../bower_components/angularAMD/angularAMD',
'ngload': '../bower_components/angularAMD/ngload',
'jquery': '../bower_components/jquery/jquery'

},

// Add angular modules that does not support AMD out of the box, put it in a shim
shim: {
'angularAMD': ['angular'],
'ngload': [ 'angularAMD' ],
'angular-route': ['angular'],
'angular-resource': ['angular']
},

// kick start application
deps: ['app']
});

app.js

define(['angularAMD', 'angular-route', 'controller/login', 'controller/project_detail', 'controller/project_list'], function (angularAMD) {
'use strict';

var app = angular.module('cmsApp', ['ngRoute']);

app.constant('REMOTE_BASE_URL', "/cms/v2/remote");

app.constant('SERVER_ERROR_TYPES', {
authentication: 'Authentication',
application: 'Application',
transport: 'Transport'
});

app.constant('AUTH_ERROR_TYPES', {
invalidLogin: "INVALID_CREDENTIALS",
invalidToken: "INVALID_TOKEN",
noToken: "NO_TOKEN"
});

app.constant('AUTH_EVENTS', {
loginSuccess: 'auth-login-success',
loginFailed: 'auth-login-failed',
logoutSuccess: 'auth-logout-success',
notAuthenticated: 'auth-not-authenticated'
});

app.config(['$routeProvider',
function($routeProvider) {
$routeProvider.
when('/login', {
templateUrl: 'partials/login.html',
controller: 'LoginCtrl'
}).
when('/projects', {
templateUrl: 'partials/project-list.html',
controller: 'ProjectListCtrl'
}).
when('/projects/:projectId', {
templateUrl: 'partials/project-detail.html',
controller: 'ProjectDetailCtrl'
}).
otherwise({
redirectTo: '/projects'
});
}]);

return angularAMD.bootstrap(app);
});

以及引发异常的文件:login_form.js

define(['app'], function (app) {
app.directive('loginForm', function (AUTH_EVENTS) {
return {
restrict: 'A',
template: '<div ng-if="visible" ng-include="\'partials/login.html\'">',
link: function (scope) {
scope.visible = false;

scope.$on(AUTH_EVENTS.notAuthenticated, function () {
scope.visible = true;
});

scope.$on(AUTH_EVENTS.loginFailed, function () {
alert("An error occured while trying to login. Please try again.")
scope.visible = true;
});

scope.$on(AUTH_EVENTS.logoutSuccess, function () {
scope.visible = true;
});
}
};
});
});

最佳答案

您正在创建应用程序本身之前加载“ Controller /登录”。

可能最好创建一个单独的模块,例如

define(['directive/login_form', 'service/authentication'], function () {
'use strict';
var loginModule = angular.module('loginModule', []);
loginModule.controller('LoginCtrl', ...
loginModule.directive('loginForm', ...

然后做类似的事情

   var app = angular.module('cmsApp', ['ngRoute', 'loginModule']);

这有意义吗?

更新:

我只是在考虑另一个解决方案。只需从您的应用程序定义中删除“ Controller /登录”即可。使用 angularAMD,在导航到指定的 url 之前,无论如何都不应该加载 Controller 。只需将其移除,您的 Controller 就会按需加载。这样,app 就被定义了! (尽管我仍然建议创建多个模块。最好不要将所有内容都放在应用程序模块中,而是为不同的职责提供多个模块。对于测试也更好。)

angularAMD.route({
templateUrl: 'views/home.html',
controller: 'HomeController',
controllerUrl: 'scripts/controller'
})

请注意字段controllerUrl

看看here .

关于angularjs - 通过 AngularAMD 将 AngularJS 与 RequireJS 结合使用 : Cannot read property 'directive' of undefined,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26744903/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com