在angularjs项目中,我想从rails服务器获取用户数据,然后使用数据。为此,我有record.js
和sessionService.js
。我从record.js
向sessionService.js
发送请求,然后接收回复。
我将js
文件和数据回显如下:
record.js
:
'use strict';
var app = angular.module('app');
app.controller('RecordCtrl',['$scope','Session','Records',function($scope, Session, Records){
$scope.user = Session.requestCurrentUser();
//echo $scope.user:
//[object Object]
}]);
sessionService.js
:
'use strict';
angular.module('sessionService', [])
.factory('Session', function($location, $http, $q) {
requestCurrentUser: function() {
if (service.isAuthenticated()) {
return $q.when(service.currentUser);
//echo service.currentUser:
//Object {then: function, catch: function, finally: function}
//catch: function (a){return this.then(null,a)}
//arguments: (...)
//get arguments: function ThrowTypeError() { [native code] }
//set arguments: function ThrowTypeError() { [native code] }
//caller: (...)
//get caller: function ThrowTypeError() { [native code] }
//set caller: function ThrowTypeError() { [native code] }
//length: 1
//name: ""
//prototype: Object
//constructor: function (a){return this.then(null,a)}
//__proto__: Object
//__proto__: function Empty() {}
//<function scope>
//finally: function (a){function b(a,c){var d=e();c?d.resolve(a):d.reject(a);return d.promise}function d(e,f){var g=null;try{g=
//then: function (b,f,h){var m=e(),I=function(d){try{m.resolve((P(b)?b:c)(d))}catch(e){m.reject(e),a(e)}},y=function(b){try{m.resolve((P(f)?f:d)(b))}catch(c){m.reject(c),a(c)}},F=function(b){try{m.notify((P(h)?h:c)(b))}catch(d){a(d)}};g?g.push([I,y,F]):l.then(I,y,F);return m.promise}
//__proto__: Object
} else {
return $http.get('/api/users').then(function(response) {
service.currentUser = response.data.user;
return service.currentUser;
//echo service.currentUser:
//Object {id: 2, email: "mgh@mgh.com", created_at: "2014-08-11T08:37:59.981Z", updated_at: "2014-08-25T09:24:53.919Z"}
});
}
},
currentUser: null,
isAuthenticated: function(){
return !!service.currentUser;
}
};
return service;
});
我不知道为什么我可以在sessionService
中查看和查看数据,但我无法在record.js
中收到数据并使用它。我该如何解决这个问题?
修改:添加app.js
:
'use strict';
angular.module('app',['ngRoute', 'ngResource', 'sessionService', 'recordService'])
.config(['$httpProvider', function($httpProvider){
$httpProvider.defaults.headers.common['X-CSRF-Token'] = $('meta[name=csrf-token]').attr('content');
var interceptor = ['$location', '$rootScope', '$q', function($location, $rootScope, $q) {
function success(response) {
return response
};
function error(response) {
if (response.status == 401) {
$rootScope.$broadcast('event:unauthorized');
scope.$apply(function() { $location.path("/route"); });
console.log('app 401');
$location.path('/users/login');
// $window.location.href = 'http://www.google.com';
return response;
};
return $q.reject(response);
};
return function(promise) {
return promise.then(success, error);
};
}];
// $httpProvider.responseInterceptors.push(interceptor);
$httpProvider.interceptors.push(interceptor);
}])
.config(['$routeProvider',function($routeProvider, $locationProvider){
$routeProvider
.when('/', {controller: 'HomeCtrl', templateUrl: '<%= asset_path('templates/index.html') %>'})
.when('/record', {controller: 'RecordCtrl', templateUrl: '<%= asset_path('templates/record/index.html') %>'})
.when('/users/login', {controller: 'UsersCtrl', templateUrl: '<%= asset_path('templates/users/login.html') %>'})
.when('/users/register', {controller: 'UsersCtrl', templateUrl: '<%= asset_path('templates/users/register.html') %>'})
.otherwise( {redirectTo: '/'});
}]);
答案 0 :(得分:0)
不确定,但您需要在模块级别中包含依赖项:
var app = angular.module('app', ['sessionService']);
我认为你有两个不同的模块,你需要其他模块中的数据,然后你需要提供依赖项。
由于您已在其他文件中使用不同模块定义Session
工厂,并且您正在使用其他模块访问其他文件,因此如果您想访问其他模块的方法,则需要传递依赖项模块语法的数组部分。