【问题标题】:How to redirect and pass data in angular js with post method如何使用post方法在angular js中重定向和传递数据
【发布时间】:2016-11-25 15:01:12
【问题描述】:

我想重定向并将一些数据传递到其他页面。但不使用查询字符串并且不在 URL 中传递数据

(function() {
var app = angular.module('PlateformApp', [])


    app.controller('PlateformController', function ($scope,$window) {
        //This will hide the DIV by default.
        $scope.IsVisible = false;
        $scope.ShowHide = function (platform) {
            //If DIV is visible it will be hidden and vice versa.
            $scope.IsVisible = $scope.IsVisible ? true : true;
            //alert(platform);
            document.getElementById("platform").value = platform;
            var myEl = angular.element( document.querySelector( '#plat_val' ) );
            myEl.text(platform); 
        }

        $scope.storeAppWindow = function()
        {

            //store_url = $scope.storeUrl;
            test_bc_url = ""
            text_sh_url = ""
            platform_val = document.getElementById("platform").value;

            $http.get("/user/installedapp")
              .then(function(response) {
                  $scope.myWelcome = response.data;
            });

            if (platform_val == "BC")
                $window.open(test_bc_url,  "popup", "width=500,height=400,left=10,top=50");
            else if (platform_val == "Sh")
                $window.open(text_sh_url,  "popup", "width=500,height=400,left=10,top=50");

        }
    });

})();

在这里它将打开新窗口,但我想在另一个页面中传递 platform_val text_sh_url url。我在 python 中使用烧瓶。

【问题讨论】:

  • 它只是将数据传递给angularjs中的另一个控制器吗??
  • 我要发送数据到其他页面
  • 这只是意味着您需要将数据传递给该特定页面的控制器,您可以使用本地存储或通过创建服务来完成。
  • 如果可能的话,你能给我一个如何创建服务的例子。
  • 很抱歉@piyush 回复晚了,这真的很简单,所以我想你可能已经明白了。反正我已经在下面回答了,希望对你有帮助

标签: python angularjs flask


【解决方案1】:

您可以通过各种方法传递数据,使用$stateParams,使用Storages,使用factories or services。您可以在我的回答中找到使用存储的示例:sharing data using storages

(function() {
  var app = angular.module('PlateformApp', [])
  app.factory('newService', function() {
    function set(data) {
      datatosend = data;
    }

    function get() {
      return datatosend;
    }

    return {
      set: set,
      get: get
    }
  });
  app.controller('PlateformController', function($scope, $window, newService) {
    //This will hide the DIV by default.
    $scope.IsVisible = false;
    $scope.ShowHide = function(platform) {
      //If DIV is visible it will be hidden and vice versa.
      $scope.IsVisible = $scope.IsVisible ? true : true;
      //alert(platform);
      document.getElementById("platform").value = platform;
      var myEl = angular.element(document.querySelector('#plat_val'));
      myEl.text(platform);
    }

    $scope.storeAppWindow = function()
    {

      //store_url = $scope.storeUrl;
      test_bc_url = ""
      text_sh_url = ""
      platform_val = document.getElementById("platform").value;

      $http.get("/user/installedapp")
        .then(function(response) {
          $scope.myWelcome = response.data;
        });

      if (platform_val == "BC") {
        $window.open(test_bc_url, "popup", "width=500,height=400,left=10,top=50");
      }
      else if (platform_val == "Sh") {
        var data = {
          'platform_val': platform_val,
          'text_sh_url ': text_sh_url
        };
        newService.set(data);
        $window.open(text_sh_url, "popup", "width=500,height=400,left=10,top=50");
      }

    }
  });

})();

并且,在您要获取数据的页面中。只需在该页面的控制器中注入 newService 并使用newService.get(data)

app.controller('pageController',function($scope,newService){
   var datafromanothercontroller = newService.get(data); 
   console.log(datafromanothercontroller );
})

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-06-03
    • 1970-01-01
    • 1970-01-01
    • 2020-09-29
    • 2021-08-03
    • 2019-01-01
    • 2021-04-18
    • 2023-01-05
    相关资源
    最近更新 更多