【问题标题】:Redirect to an html page using angular使用角度重定向到 html 页面
【发布时间】:2016-09-16 14:37:01
【问题描述】:

我正在尝试在单击按钮时使用角度从 productlisting.html 页面重定向到 search.html,但 URL 栏中附加的 URL 是:

/dashboard-html/product-listing1.html#/#%2Fsearch.html%3Fquery=biscuits

我希望它是这样的

/dashboard-html/search.html%?query=biscuits

我的代码script.js 是:

Array.prototype.chunk = function (groupsize) {
    var sets = [];
    var chunks = this.length / groupsize;

    for (var i = 0, j = 0; i < chunks; i++, j += groupsize) {
      sets[i] = this.slice(j, j + groupsize);
    }

    return sets;
};

var data_bind=angular.module('my_app', []);
data_bind.controller('productController', function ($scope, $http, $location)
{ 
        var isProductListingpage = false;

        $scope.init = function (isProductListpage){
            isProductListingpage = isProductListpage;

            // if this is not search page, get the query params from url and get the products to show from server
            if(!isProductListingpage)
            {
                var queryParam = location.search;
                var request_url='http://dmsp1-kakash.boxfuse.io:9000/products?'+queryParam;
                sendRequest(request_url);
            }
        }

        function sendRequest(request_url)
        {
             $http({
                    method : 'GET',
                    url : request_url,
                    headers : {'Content-Type' : 'application/json'}
                   }).success(function(response){

                           if(response.errors){
                               $scope.errorName = response.errors.name ;
                               $scope.errorUserName = response.errors.username;
                               $scope.errorEmail = response.errors.email;
                           }
                           else
                           {
                               $scope.product = response.data.products;
                                $scope.productGroups = $scope.product.chunk(3);
                           }
                   });
        }

         $scope.search_field={};
         $scope.searchdata= function()
         {
                var queryParam = "query="+encodeURI($scope.search_field.search);
                var request_url='http://dmsp1-kakash.boxfuse.io:9000/products?'+queryParam;
                // If this is product listing page, we need to redirect to search page,
                 // otherwise make a request to server
                 if(isProductListingpage) {
                     //$window.location.href = '/index.html'
                     $location.url('#/search.html?'+queryParam);
                 }
                 else {
                    sendRequest(request_url);
                 }

         }
         $scope.search_field1={};
         $scope.search_page= function(event)
         {
             var $target = $(event.target);
             if($target.hasClass('sub-category'))
             {
                 var subCategory = $target.clone()    //clone the element
                                          .children() //select all the children
                                          .remove()   //remove all the children
                                          .end()  //again go back to selected element
                                          .text();
                 var $temp = $target.closest('.submenu');
                 var $mainCategoryElement = $temp.siblings('.main-category').first();
                 var mainCategory = $mainCategoryElement.text();

                 // Encode both the values
                 subCategory = encodeURI(subCategory);
                 mainCategory = encodeURI(mainCategory);
                 var queryParam = 'cat='+mainCategory+"&subcat="+subCategory;
                 var request_url='http://dmsp1-kakash.boxfuse.io:9000/products?'+queryParam;

                 // If this is product listing page, we need to redirect to search page,
                 // otherwise make a request to server
                 if(isProductListingpage) {
                     $location.url('#/search.html?'+queryParam);
                 }
                 else {
                    sendRequest(request_url);
                 }
             }
         }

         });``

【问题讨论】:

    标签: javascript angularjs url url-rewriting


    【解决方案1】:

    您应该使用默认的角度路由器或 ui-router 并定义清晰的状态。

    【讨论】:

    • 你试过使用 ui-router 吗?还是默认的角度路由器?我相信你的方法是错误的。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-11-29
    • 2019-06-23
    • 2012-06-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多