AngularJS : Pass data to state with $state.go in angular-ui-router

前端 未结 2 1093
臣服心动
臣服心动 2020-12-16 02:26

I\'m making a document editor. Documents can be Type A or Type B. They are accessed by url by document id, but the id does not make it clear if the document is of type A or

2条回答
  •  清酒与你
    2020-12-16 03:21

    You could have a Documents service which owns and provides an API to all the document data. The controllers then inject the Documents service, and reference the document they're interested in on their scope.

    Something like:

    app.service('Documents', function(){
      Documents = {};
    
      return {
        open: function(doc_id) { ... }  // loads and caches doc (if not already cached) and returns a promise with a reference to Documents[doc_id]
        sync: function(doc_id) { ... }  // sync doc with server
        close: function(doc_id) { ... } // remove doc_id from Documents 
      };
    });
    
    app.controller('editX', function($scope, $stateParams, Documents){
      Documents.open($stateParams.documentId).then(function(ref){
        $scope.document = ref;
      });
    
      ...
    
    })
    

提交回复
热议问题