profile.html
<section data-ng-controller="MyprofileController">
<h1>{{global.current_User()}}</h1>
</section>
myprofile.js
'use strict';
angular.module('mean.system').controller('MyprofileController', ['$scope', 'Global', function ($scope, Global) {
$scope.global = Global;
$scope.test = 'testcase';}]);
服务
'use strict';
//Global service for global variables
angular.module('mean.system').factory('Global', [
function() {
var current_user = window.user;
return {
current_User: function() {
return current_user;
},
isloggedIn: function() {
return !!current_user;
}
};
}
]);
非常感谢你的帮助。
刚刚发现firefox会打印错误信息!
Error: [ng:areq] Argument 'MyprofileController' is not a function, got undefined
http://errors.angularjs.org/1.2.11/ng/areq?
p0=MyprofileController&p1=not%20a%20function%2C%20got%20undefined
minErr/<@http://localhost:3000/lib/angular/angular.js:78
assertArg@http://localhost:3000/lib/angular/angular.js:1363
assertArgFn@http://localhost:3000/lib/angular/angular.js:1374
@http://localhost:3000/lib/angular/angular.js:6774
nodeLinkFn/<@http://localhost:3000/lib/angular/angular.js:6186
forEach@http://localhost:3000/lib/angular/angular.js:310
nodeLinkFn@http://localhost:3000/lib/angular/angular.js:6173
compositeLinkFn@http://localhost:3000/lib/angular/angular.js:5637
publicLinkFn@http://localhost:3000/lib/angular/angular.js:5542
ngViewFillContentFactory/<.link@http://localhost:3000/lib/angular-route/angular-
route.js:915
nodeLinkFn@http://localhost:3000/lib/angular/angular.js:6228
compositeLinkFn@http://localhost:3000/lib/angular/angular.js:5637
publicLinkFn@http://localhost:3000/lib/angular/angular.js:5542
boundTranscludeFn@http://localhost:3000/lib/angular/angular.js:5656
controllersBoundTransclude@http://localhost:3000/lib/angular/angular.js:6248
update@http://localhost:3000/lib/angular-route/angular-route.js:865
Scope.prototype.$broadcast@http://localhost:3000/lib/angular/angular.js:12245
updateRoute/<@http://localhost:3000/lib/angular-route/angular-route.js:556
qFactory/defer/deferred.promise.then/wrappedCallback@http:
//localhost:3000/lib/angular/angu lar.js:10949
qFactory/defer/deferred.promise.then/wrappedCallback@http:
//localhost:3000/lib/angular/angu lar.js:10949
qFactory/ref/<.then/<@http://localhost:3000/lib/angular/angular.js:11035
Scope.prototype.$eval@http://localhost:3000/lib/angular/angular.js:11961
Scope.prototype.$digest@http://localhost:3000/lib/angular/angular.js:11787
Scope.prototype.$apply@http://localhost:3000/lib/angular/angular.js:12067
@http://localhost:3000/lib/angular/angular.js:9202
createEventHandler/eventHandler/<@http://localhost:3000/lib/angular/angular.js:2613
forEach@http://localhost:3000/lib/angular/angular.js:310
createEventHandler/eventHandler@http://localhost:3000/lib/angular/angular.js:2612
<section class="ng-scope" data-ng-view="">
答案 0 :(得分:0)
它应该有效,并且它在我创建的小提琴中完成:http://jsfiddle.net/BernhardW/mLQWs/
window.user = 'John Doe';
angular.module('mean.system', []);
angular.module('mean.system').controller('MyprofileController', function ($scope, Global) {
$scope.global = Global;
$scope.test = 'testcase';
});
angular.module('mean.system').factory('Global', function() {
var current_user = window.user;
return {
current_User: function() {
return current_user;
},
isloggedIn: function() {
return !!current_user;
}
};
});
是否有错误显示?