控制器间的角度服务

时间:2016-04-21 09:10:07

标签: javascript angularjs angular-services angular-controller

我的服务在加载时为空:

app.factory("bookmark",function(){
    var bookmark = {};
    return bookmark;
});

我有控制器A设置服务的值并重定向到第二个控制器,我尝试访问空的服务值。

控制器A:

app.controller('bookmarksController', function($scope, localStorageService, bookmark) {
  // localStorageService.clearAll();

  $scope.bookmarks = localStorageService.keys();
  $scope.setBookmarkServ = function (key){
    console.log('bookmark service set');

    bookmark = key;
    console.log(bookmark);

  }

  $scope.go2bookmark = function (key){

    console.log('go2bookmark called');

    var obj = localStorageService.get(key);
    return obj.link;
  }
});

模板A:

   <script type="text/ng-template" id="bookmarks">
    <div ng-controller="bookmarksController">
      <hr style="clear:both;opacity:0;" />
        <div class="logo"></div>
        <h1>Bookmarks</h1>
        <div ng-repeat="mark in bookmarks" class="col-xs-12 col-sm-6 col-md-3">
             <a ng-click="setBookmarkServ(mark)" ng-init="theLink = go2bookmark(mark)" href="#{{theLink}}" class="thumbnail alert-info" style="height:150px;">
              <div class="caption">
                <h3>{{ mark }}</h3>
                {{theLink}}
              </div>
             </a>
        </div>
     </div>  
    </script>

控制器B,其中服务为空}:

app.controller('baselistController', ['$scope', '$routeParams', '$route', '$http', 'customview', '$location', '$uibModal', '$cacheFactory', '$templateCache', 'columnselector', '$interval', '$rootScope', 'globalVarService', '$window', '$controller', '$timeout', 'localStorageService', 'bookmark',

  function ($scope, $routeParams, $route, $http, customview, $location, $uibModal, $cacheFactory, $templateCache, columnselector,  $interval, $rootScope, globalVarService, $window, $controller, $timeout, localStorageService, bookmark) {

    //set value doesn't perist
    console.log(bookmark);
    ...
    ...

3 个答案:

答案 0 :(得分:4)

app.factory("bookmark",function(){
    var _bookmark = {};
    var setBookMark = function(key){
        _bookmark = key;
    }

    var getBookMark = function(){
        return _bookmark;
    }
    return {
        GetBookMark = getBookMark,
        SetBookMark = setBookMark 
    };
});

现在您可以使用此服务来设置和获取书签,

在控制器A中,

app.controller('bookmarksController', function($scope, localStorageService, bookmark) {
      bookmark.SetBookMark(localStorageService.keys(););
}

在控制器B中获取书签,

app.controller('baselistController', ['$scope', '$routeParams', '$route', '$http', 'customview', '$location', '$uibModal', '$cacheFactory', '$templateCache', 'columnselector', '$interval', '$rootScope', 'globalVarService', '$window', '$controller', '$timeout', 'localStorageService', 'bookmark',
  function ($scope, $routeParams, $route, $http, customview, $location, $uibModal, $cacheFactory, $templateCache, columnselector,  $interval, $rootScope, globalVarService, $window, $controller, $timeout, localStorageService, bookmark) {

    console.log(bookmark.GetBookMark());
}

答案 1 :(得分:4)

@Abhilash给出了解决方案,让我解释原因。

您需要了解passed by referencepassed by valueWhat's the difference between passing by reference vs. passing by value?之间的区别。

您定义了服务bookmark,实际上就像是:

enter image description here

您已将服务bookmark注入您的控制器。在这种情况下,bookmark是参考。当你执行bookmark = key;时,你丢失了对内存中真实对象的引用:

enter image description here

这就是为什么您的服务无法在控制器之间共享的原因。因为你的修改实际上没有影响。

解决方案:请参阅@ Abhilash的回答。

答案 2 :(得分:-2)

而不是

 $scope.bookmarks = localStorageService.keys();

将书签保存在

 $rootScope.bookmarks = localStorageService.keys();

所以他们将在整个应用中分享。