【问题标题】:using ngStorage as array in ionic在 ionic 中使用 ngStorage 作为数组
【发布时间】:2015-10-27 06:22:26
【问题描述】:

试图转换这个

.service('dataStore', function($localStorage,$scope){
    this.entfeeds=[];
    this.topfeeds=[];
    this.intfeeds=[];
})
.controller('GenFeedCtrl', function ($scope,....
     $scope.feeds = FeedList.get(feedSources, dataStore.topfeeds);

有效,除了在手机cordova上运行时,它只返回两个项目

所以我尝试使用 ngStorage 来存储数组

.service('dataStore', function($localStorage,$scope){
    $scope.$storage = $localStorage.$default({
        etf:[],
        tpf:[],
        itf:[]
    });

    this.entfeeds=$storage.etf;
    this.topfeeds=$storage.tpf;
    this.intfeeds=$storage.itf;})

    .controller('GenFeedCtrl', function ($scope,....
     $scope.feeds = FeedList.get(feedSources, dataStore.topfeeds);

但不能在浏览器emu上运行,会出现以下错误

Error: [$injector:unpr] Unknown provider: $scopeProvider <- $scope <-     dataStore

http://errors.angularjs.org/1.3.13/$injector/unpr?p0=copeProvider%20%3C-%20%24scope%20%3C-%dataStore 在http://localhost:8100/lib/ionic/js/ionic.bundle.js:8762:12

【问题讨论】:

    标签: angularjs cordova ionic ng-storage


    【解决方案1】:

    服务中没有$scope$scope 仅在视图相关组件中需要,并且服务与视图无关。

    $rootScope 可以注入到服务中,但不适合存储数据。它更常用于广播事件的服务中

    你可以使用变量来定义不需要直接绑定到this的东西

    .service('dataStore', function($localStorage){
        var $storage = $localStorage.$default({
            etf:[],
            tpf:[],
            itf:[]
        });
    
        this.entfeeds=$storage.etf;
        this.topfeeds=$storage.tpf;
        this.intfeeds=$storage.itf;
    })
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-10-25
      • 2016-01-07
      • 1970-01-01
      • 1970-01-01
      • 2016-07-29
      • 1970-01-01
      • 2023-03-15
      • 2018-02-26
      相关资源
      最近更新 更多