简体   繁体   English

将数据从角度形式传递到另一个控制器

[英]Pass data from angular form to another controller

I am new to angularjs. 我是angularjs的新手。 I want to pass data from html form to another route. 我想将数据从html表单传递到另一条路由。

Here is the part of index.html 这是index.html的一部分

<div ng-app="myApp" ng-controller="HomeController">
    <div class="container">
        <div class="row">
            <div class="col-md-12">
                <div ng-view=""></div>
            </div>
        </div>
    </div>
</div>

Here are the routes 这是路线

var myApp = angular.module('myApp', ['ngRoute']);
myApp.config(['$routeProvider', function ($routeProvider) {
   $routeProvider.when('/', {
   templateUrl: 'views/home.html',
   controller: 'HomeController'
});
$routeProvider.when('/about', {
  templateUrl: 'views/about.html',
  controller: 'AboutController'
});
}]);

When the route is / it hits the views/home.html in which it has a form 当路线为/时,它会到达带有表单的views / home.html

<form action="#/about" ng-submit="submitData()">
    <input type="text" name="address" ng-model="user.address" />
    <input type="submit" />
</form>

I have a user service whose implementation is 我有一个用户服务,其实现是

myApp.factory("user", function () {
     return {};
});

I inject user service in HomeController like 我在HomeController中注入用户服务

 myApp.controller("HomeController", function ($scope, user) {
          $scope.user = user;
          // and then set values on the object

          // $scope.user.address = "1, Mars";    // when uncomment this line it can be accessed on AboutController? Why? Otherwise I cannot access user.address
          console.log($scope.user);
  });

Don note my comment in above code.. 不要在上面的代码中记下我的评论。

and passes user to AboutController like 并将用户传递给AboutController

myApp.controller("AboutController", function ($scope, user) {
    $scope.user = user;
    // and then set values on the object
    $scope.user.firstname = "John";
    $scope.user.secondname = "Smith";
    console.log($scope.user);
});

Here is the about.html 这是about.html

<p>
    {{ user.address }}
</p>

Problem: {{user.address}} doesn't work on AboutController . 问题: {{user.address}}在AboutController不起作用 I can't see any output... But when i remove the comment from above code.. It only displays hardcoded values in the controller What am I missing? 我看不到任何输出...但是当我从上面的代码中删除注释时。。它仅在控制器中显示硬编码的值,我缺少什么?

This is the working demo http://ashoo.com.au/angular/#/ 这是工作中的演示http://ashoo.com.au/angular/#/

At the moment, all your service does is pass a blank object return {} , to any controller into which it is injected. 目前,您所做的所有服务就是将空白对象return {}传递给注入该对象的任何控制器。 You need a getter/setter approach, so that in your Home view you can set the data, and in your About view you can retrieve it. 您需要一种getter / setter方法,以便在“ Home视图中可以设置数据,在“ About视图中可以检索数据。

So your service could look something like this: 因此,您的服务可能如下所示:

myApp.factory("user", function () {

     var dataObj = {};

     return {
            setData: function(data) {
               dataObj.username = data.username;
            },
            getData: function() {
               return dataObj;
            }
     };
});

Now you can set the data in your Home controller: 现在,您可以在Home控制器中设置数据:

myApp.controller("HomeController", function ($scope, user) {

    $scope.submitData = function(data)  {  //pass in ng-model
       user.setData(data);  //call 'user' service to set data
    }
  });

And call it from your About controller: 并从About控制器中调用它:

myApp.controller("AboutController", function ($scope, user) {
    $scope.user = user.getData();  //Assign 
    console.log($scope.user.username);
});

And you html would look like: 而且您的html看起来像:

<form action="#/about" ng-submit="submitData(user.username)">
    <input type="text" name="address" ng-model="user.username" />
    <input type="submit" />
</form>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何将数据从表单传递到 angular 中的另一个组件 - How to pass data from a form into another component in angular 将数据从MVC控制器传递到角度控制器 - pass data from mvc controller to angular controller AngularJS将数据从控制器传递到另一个控制器 - AngularJS pass data from controller to another controller 在路由时在不使用服务或Factory的情况下将JSON数据从控制器传递到另一个控制器 - Pass the JSON Data Between From Controller To another Controller with out using Services or Factory in angular JS while Routing 如何将数据传递给 Angular 中另一个组件中的表单 - how to pass data to a form in another component in Angular 角度:将数据从服务/工厂传递到控制器 - Angular: pass data from service/factory to controller 如何将表单输入数据从数据列表传递到角度控制器? - How do I pass form input data from a datalist to my angular controller? 将数据从Angular模态的控制器传递回主控制器 - Pass data from an Angular modal's controller back to the main controller 在Angular Service中获取数据并将其传递给另一个控制器 - Get data in Angular Service and pass it through to another controller 角度-将信息从数组从一个控制器传递到另一个 - Angular - pass info from array from one controller to another
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM