簡體   English   中英

AngularJS:將信息從Controller傳遞給Service

[英]AngularJS: Pass info from Controller to Service

有沒有辦法將信息從控制器傳遞給服務? 我知道您應該將信息從服務傳遞給控制器​​,但讓我向您解釋一下我的問題:

我有兩個屏幕(或兩個視圖)與不同的控制器,但具有相同的模塊。 讓我們給它們命名:ControllerOne與ViewOne和ServiceOne和ControllerTwo與ViewTwo和ServiceTwo。

ServiceOne發出一個http請求來獲取一些json數據(來自遠程源)。 ControllerOne我有一個函數,它調用ServiceOne來獲取所有數據,在ViewOne中我顯示從遠程源(從ControllerOne)獲取的所有視頻的列表。

ServiceOne的代碼如上所示:

(function() {
'use strict';

angular
    .module('MyApp.MyModule')
    .factory('ServiceOne', ServiceOne);

ServiceOne.$inject = ['$http', '$q'];


function ServiceOne($http, $q) {

    var url = 'https://THE_LINK_I_WANT_TO_GET_DATA_FROM';
    var result = [];

    var service = {
        getAllVideos: getAllVideos,
        getVideo: getVideo
    };
    return service;


    function getAllVideos(callback){
        $http.get(url)
            .success(function(data) {
                result = data.result;
                callback(result);
            });
    }

    function getVideo(videoId) {
        for (var i = 0; i < result.length; i++) {
            if (result[i].videoId === videoId) {
                return $q.when(result[i]);
            }
        }
        return $q.when(null);
    }

}
})();

ControllerOne看起來像這樣:

(function() {
    'use strict';

    angular
        .module('MyApp.MyModule')
        .controller('ControllerOne', ControllerOne);

        ControllerOne.$inject = ['$scope', '$state', 'ServiceOne'];

        function ControllerOne($scope, $state, ServiceOne) {


            var vm = angular.extend(this, {
                videos: [],
                navigateToVideo: navigateToVideo
            });


            ServiceOne.getAllVideos(function(data){
                vm.videos = data;
            });


            function navigateToVideo(videoId) {
                $state.go('app.video', { videoId: videoId } );
            }


        }

})();

ViewOne ,每次有人點擊視頻時,我都會將它們導航到另一個視圖(在我的情況下為ViewTwo ),並傳遞一個參數(一個唯一的ID)。

ControllerTwo看起來像這樣:

(function() {
'use strict';

angular
    .module('MyApp.MyModule')
    .controller('ControllerTwo', ControllerTwo);

    ControllerTwo.$inject = ['$scope', '$state', '$stateParams', 'ServiceOne'];

    function ControllerTwo($scope, $state, $stateParams, ServiceOne) {


        var vm = angular.extend(this, {
            video: []
        });


        (function init(){
            loadData();
        })();


        function loadData(){    
            var videoId = String($stateParams.videoId);
            ServiceOne.getVideo(videoId)
                .then(function(video) {
                    vm.video = video;
                });  
        }


    }

})();

ControllerTwo我保存從ControllerOne傳遞的參數,並將其保存在videoId 然后我調用ServiceOne的函數來獲取我正在尋找的對象。

所以,我的問題是:

我可以將ControllerTwo的對象vm.video傳遞給ServiceTwo ,以便將來可以使用嗎?

謝謝!

服務是MADE,用於在您的應用程序中共享內容(數據和方法)。 所以,簡單地回答你的問題:絕對,是的,你可以將你的對象傳遞給你的服務。 而且,它可能不容易......

ServiceOne和ServiceTwo可以被任何控制器訪問,如果你想“傳遞一些東西”到服務,它就像這樣簡單:

ControllerOne.$inject = ['$scope', '$state', 'ServiceOne', 'ServiceTwo'];
function ControllerOne($scope, $state, ServiceOne, ServiceTwo) {

  function loadData(){    
    var videoId = String($stateParams.videoId);
    ServiceOne.getVideo(videoId)
    .then(function(video) {
      vm.video = video;
      ServiceTwo.video = video; //bam, you're done
    });  
  }
}

視頻將一直顯示,直到您重新啟動應用。

ControllerOne.$inject = ['$scope', '$state', 'ServiceOne', 'ServiceTwo'];
function ControllerOne($scope, $state, ServiceOne, ServiceTwo) {
  // now you can access it from your other controller
  var videoStoredInServiceTwo = ServiceTwo.video;
}

唯一需要注意的是時機。 在嘗試檢索視頻之前,您可能需要檢查ServiceTwo上是否存在視頻。

if( ServiceTwo.video ) {
    videoStoredInServiceTwo = ServiceTwo.video;
  }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM