我想在服务中使用$localStorage
。我将ngStorage
cdn包含在index.html
的标题部分中。
<script src="Vendor/jquery-1.11.3.min.js"></script>
<script src="Vendor/bootstrap.min.js"></script>
<script src="Vendor/angular.min.js"></script>
<script src="Vendor/angular-ui-router.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/ngStorage/0.3.6/ngStorage.min.js"></script>
savePreferenceService.js
(function() {
'use strict';
var savePreferenceService = function ($localStorage) {
this.selectedPreferences = function (selectedLang, converse, selectedNumber) {
$localStorage.selectedLang = selectedLang;
$localStorage.converse = converse;
$localStorage.selectedNumber = selectedNumber;
console.log($localStorage.selectedLang);
console.log($localStorage.converse);
console.log($localStorage.selectedNumber);
}
this.getPreferences = function () {
return this.userPreferences = {
selectedLang : '',
converse : {
state: 'on'
},
selectedNumber : 0
}
}
this.selectedPreferences = function (selectedLang, converse, selectedNumber) {
this.selectedLang = selectedLang;
this.converse = converse;
this.selectedNumber = selectedNumber;
console.log(this.selectedLang);
console.log(this.converse);
console.log(this.selectedNumber);
}
};
angular.module('iisEmail', ['ngStorage'])
.service ('savePreferenceService', ['$localStorage', savePreferenceService]);
}());
我不明白为什么我会收到ng:areq
错误。我想我没有正确注入ngStorage。有没有人对我做错了什么有想法?
更新
我从服务中删除了ngStorage
依赖项,并将其添加到main.js
。这样做解决了我遇到的错误。我认为这个错误是由于我没有在main.js
注入模块iisEmail
的依赖项中引入依赖项。
savePreferenceService
(function() {
'use strict';
var savePreferenceService = function ($localStorage) {
this.selectedPreferences = function (selectedLang, converse, selectedNumber) {
/* $localStorage.selectedLang = selectedLang;
$localStorage.converse = converse;
$localStorage.selectedNumber = selectedNumber;
console.log($localStorage.selectedLang);
console.log($localStorage.converse);
console.log($localStorage.selectedNumber);*/
}
this.getPreferences = function () {
return this.userPreferences = {
selectedLang : '',
converse : {
state: 'on'
},
selectedNumber : 0
}
}
this.selectedPreferences = function (selectedLang, converse, selectedNumber) {
this.selectedLang = selectedLang;
this.converse = converse;
this.selectedNumber = selectedNumber;
console.log(this.selectedLang);
console.log(this.converse);
console.log(this.selectedNumber);
}
};
angular.module('iisEmail')
.service ('savePreferenceService', ['$localStorage', savePreferenceService]);
}());
main.js
var app = angular.module("iisEmail",['ui.router', 'ngStorage']);
app.config(['$stateProvider','$urlRouterProvider',
function($stateProvider,$urlRouterProvider){
$urlRouterProvider.otherwise("/");
var primary = {
name:"primary",
url:"/",
templateUrl:"app/partials/emailView.html"
//controller:"emailViewCtrl"
};
var settings ={
name:"settings",
url:"/",
templateUrl:"app/partials/settings.html"
//controller:"emailViewCtrl"
};
$stateProvider.state('default',primary);
$stateProvider.state('settings',settings);
}]);