出厂值在控制器中随机更改为空,但是我将其注入控制器
the factory value is changed to be null in controller randomly, however I injected it to controller
我有一个名为 myController
的控制器,如下所示:
'use strict';
app.controller('myController', ['$rootScope', '$scope', 'myService',
function ($rootScope, $scope, myService) {
$scope.myService = myService;
$rootScope.$on('kpi:submit', function (event, data) {
$window.sessionStorage['prevCall'] = JSON.stringify(data.prevCall);
$window.sessionStorage['currCall'] = JSON.stringify(data.currentCall);
$window.sessionStorage['nextCall'] = JSON.stringify(data.nextCall);
myService.prevCall = $scope.prevCall = data.prevCall;
myService.currentCall = $scope.currentCall = data.currentCall;
myService.nextCall = $scope.nextCall = data.nextCall;
});
}]);
和 myService
工厂,如下所示:
'use strict';
app.factory('myService', ['$http', '$q', function ($http, $q) {
this.currentCall;
this.prevCall;
this.nextCall;
// some function
return {
myFunction : myFunction
};
}]);
当我单击 Submit
按钮时,将调用 kpi:submit
。它运行良好,但有时会出现以下错误:
TypeError: Cannot set property 'prevCall' of null
那么,为什么 myService
工厂突然更改为 null
,请问有什么建议的解决方案吗?
您传递给 $rootScope.$broadcast
的匿名函数没有对 myService 的引用。
要么注入你的函数:
function (event, data, myService)
继续使用 $scope:
$scope.myService.prevCall = $scope.prevCall = data.prevCall;
也可以使用绑定:
$rootScope.$on('kpi:submit', function (event, data) {
}.bind(myService));
我有一个名为 myController
的控制器,如下所示:
'use strict';
app.controller('myController', ['$rootScope', '$scope', 'myService',
function ($rootScope, $scope, myService) {
$scope.myService = myService;
$rootScope.$on('kpi:submit', function (event, data) {
$window.sessionStorage['prevCall'] = JSON.stringify(data.prevCall);
$window.sessionStorage['currCall'] = JSON.stringify(data.currentCall);
$window.sessionStorage['nextCall'] = JSON.stringify(data.nextCall);
myService.prevCall = $scope.prevCall = data.prevCall;
myService.currentCall = $scope.currentCall = data.currentCall;
myService.nextCall = $scope.nextCall = data.nextCall;
});
}]);
和 myService
工厂,如下所示:
'use strict';
app.factory('myService', ['$http', '$q', function ($http, $q) {
this.currentCall;
this.prevCall;
this.nextCall;
// some function
return {
myFunction : myFunction
};
}]);
当我单击 Submit
按钮时,将调用 kpi:submit
。它运行良好,但有时会出现以下错误:
TypeError: Cannot set property 'prevCall' of null
那么,为什么 myService
工厂突然更改为 null
,请问有什么建议的解决方案吗?
您传递给 $rootScope.$broadcast
的匿名函数没有对 myService 的引用。
要么注入你的函数:
function (event, data, myService)
继续使用 $scope:
$scope.myService.prevCall = $scope.prevCall = data.prevCall;
也可以使用绑定:
$rootScope.$on('kpi:submit', function (event, data) {
}.bind(myService));