AngularJS Service在路由之间丢失值

时间:2015-12-04 10:21:14

标签: angularjs angular-services

我有2个不同的控制器访问相同的服务。其中一个控制器search.ctrl.js在每个页面(页面顶部的搜索栏)上都可见,另一个控制器用于特定的部分控制器,在表格中显示搜索结果。

我遇到的问题是服务在提交搜索表单后正确设置值,并且这些值会正确地传递回控制器,但是当我调用$location.path('/');时值为no更长的服务设置。

这是否是正确的方法,我想要的是能够在每个页面上使用相同的html / js / css代码的搜索栏,并且可以从特定的方法访问搜索结果页。随意建议替代解决方案,但我的首选答案是修复此问题,因为我已经读过通常最好使用服务在视图之间共享数据。

查看以下代码时请注意,我希望从多个视图中访问documents

干杯:)

控制器

search.ctrl.js

angular.module("app").controller("SearchController", 
  function($scope, $location, DocumentSearch){
    $scope.title = "Some title";
    $scope.doctypes = [
                       {type: "doc", checked: true},
                       {type: "docx", checked: true},
                       {type: "xls", checked: true},
                       {type: "xlsx", checked: true},
                       {type: "txt", checked: true}];

    $scope.checkAll = true;
    $scope.searching = false;
    $scope.showSearchPanel = function(){
        $scope.searching = !$scope.searching;
    };
    $scope.checkOrUncheckAll = function(checkAll){
        $scope.checkAll = checkAll;
        setValueOfAllCheckBoxes($scope.checkAll);
    };

    $scope.someSelected = function (doctypes) {
        for (var d in doctypes){
            console.log(doctypes[d]);
            if (doctypes[d].checked)
                return true;
        }

        return "You must select a checkbox";
    };

    $scope.search = function(documentTitle, checkAll){
        if (atLeastOneCheckBoxIsSelected()){
            $scope.error = null;
            $scope.documents = DocumentSearch.search(documentTitle, 
                               getSelectedCheckBoxes());
            $location.path("home");
        } else {
            $scope.documents = null;
            $scope.error = "Please select at least one file type.";
        }
    };

    function setValueOfAllCheckBoxes(isChecked){
        for (var d in $scope.doctypes){
            $scope.doctypes[d].checked = isChecked;
        }
    }

    function atLeastOneCheckBoxIsSelected(){
        return (getSelectedCheckBoxes().length > 0);
    }

    function getSelectedCheckBoxes(){
        return $scope.doctypes.filter(function(obj){
            return obj.checked;
        });
    }

});

main.ctrl.js

angular.module("app").controller("MainController", 
    function($scope, $location, DocumentSearch){
        $scope.documents = DocumentSearch.documents;
        console.log(DocumentSearch);
});

search.ser.js

angular.module("app").factory("DocumentSearch", function(){

    var search = function(title, doctypes){
        return searchExample(title, doctypes);
    };

    function getDocumentExtention(checked){

        checked.sort( function(){ return 0.5 - Math.random(); } );

        return checked[0].type;
    }

    console.log("SOMETHING");
    var documents = searchExample("Example document", [{type: "txt", checked: true}]);

    function searchExample(title, doctypes){
        documents = [];
        for (var i = 0; i < 50; i++) {
            documents.push({
                documentTitle: title + " " + i + "." + getDocumentExtention(doctypes),
                documentID: i * 111111111,
                documentStatus: "Available",
                documentVersionDate: "15:32:254 18/11/2015",
                documentFileModified: "15:35:974 18/11/2015",
                documentCheckedOutUserID: "8276",
                documentCheckedOutUsername: "joshuaBrass1"
            });
        }

        return documents;
    }

    return {
        search: search,
        documents: documents
    };
});

0 个答案:

没有答案