Pass data from angular form to another controller

≡放荡痞女 提交于 2021-02-08 11:19:31

问题


I am new to angularjs. I want to pass data from html form to another route.

Here is the part of 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

<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

 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

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

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

Problem: {{user.address}} doesn't work on 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/#/


回答1:


At the moment, all your service does is pass a blank object return {}, to any controller into which it is injected. 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.

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:

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:

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

And you html would look like:

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


来源:https://stackoverflow.com/questions/35628027/pass-data-from-angular-form-to-another-controller

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!