javascript - 当页面更改时,Angular Controller 将数据传递给其他 Controller

标签 javascript html angularjs angularjs-controller

嗨,我有一个控制所有 Controller 的 Java 脚本文件。我还有两个 html 页面,在提交表单时相互链接,当按下提交按钮时,我需要将一个值传递给另一个 Controller 。

如果我不定向到另一个页面,我可以记录该值,但是一旦我定向到另一个页面,值就会变为“未定义”

这是我的两个 Angular Controller

app.controller('createworkshopController', function($rootScope, $scope, $http) {

    //get bunch of stuff

    $scope.submitWorkshop = function() {    

      $http({method:'POST', data:$scope.result, url: 'url for post method'})
          .success(function(data) {
              $rootScope.ID = data.value;
              console.log($rootScope.ID); //this log return the value
          });
      };
});

但是当我重定向到其他页面时,该值变得未定义 这是第二页的 Controller

app.controller('showworkshopController', function($rootScope, $scope, $http) {

    console.log($rootScope.ID); //this becomes undefined

    $http.get('some_url?Id=' + $rootScope.ID)
    .success(function(data) {
        //do bunch of stuff
    });

    //do bunch of stuff
});

我的第一页 html 有一个这样写的提交按钮

    <a href="second-page.html" type="submit" class="btn btn-default" ng-click="submitWorkshop()">Submit</a>

最佳答案

您的问题是您的应用程序将在页面刷新/重定向时重新初始化。

您需要将其构建为单页应用程序,而不进行重定向,或者在重定向之前将值存储在 Cookie 或本地存储中,并在加载第二页时进行检索。

基本上,Angular 实际上是用于构建应用程序,而不是作为标准 Web 表单的添加。

关于javascript - 当页面更改时,Angular Controller 将数据传递给其他 Controller ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32062205/

相关文章:

php - 从登录页面将数据提交到 MySQL 数据库的代码

html - 强制分为 2 列的列表 - IE7

html - 换行时减小angularjs中的文本大小?

javascript - TypeError:event.preventdefault不是函数

javascript - Electron App 中的 BrowserWindow 渲染

javascript - 网站淡入淡出效果在 Internet Explorer 中不起作用

javascript - 如何从 Angular 中的其他函数访问工厂函数

javascript - 带有生成字段的 Ng-repeat 提交表单

javascript - 如何在模板中组织 Canvas 脚本

javascript - 滚动 Dropdown li 元素直到元素可见