1

伙计们,我遇到了数据绑定问题。多年来,我一直试图弄清楚为什么我的少数人无法访问服务提供的 global.user。有人可以弄清楚发生了什么。预先感谢。最好的问候托马斯

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="">
4

1 回答 1

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;
        }
    };
});

是否有任何错误显示?

于 2014-02-09T14:41:09.267 回答