Skip to content Skip to sidebar Skip to footer

Passing Values In Angular Js

I'm a newbie to Angular JS. I've created a form in my index.html page, when I fill the details in the form and press submit, it should redirect to details.html page. Where I can ab

Solution 1:

You can achieve this by adding angular routing to your application which need ngRoute dependency. Then you need to define one parent controller that can hold the partial views common data like here it is mainCtrl.

Apart from that you missed few things while you created a form, form should have its name attribute so that angular will create a scope variable of that form and internally manages form validity inside that scope variable like $valid, $error, etc. Then the same name should be given to each form element, if you don't declare the name attribute, then it won't consider it as form element.

HTML

<htmlng-app="app"><head><scripttype="text/javascript"src="http://code.angularjs.org/1.2.13/angular.js"></script><scripttype="text/javascript"src="http://code.angularjs.org/1.2.13/angular-route.js"></script><scriptsrc="example.js"></script></head><body><divng-controller="mainCtrl"><divclass="forms"><ng-view></ng-view></div></div></body></html>

CODE

var app = angular.module('app', ['ngRoute']);
app.config(function($routeProvider) {
  $routeProvider
    .when('/view1', {
      templateUrl: 'view1.html',
      controller: 'CustomerDetailsController'
    })
    .when('/view2', {
      templateUrl: 'view2.html',
      controller: 'form2Ctrl'
    })
    .otherwise({
      redirectTo: '/view1'
    });
});
app.controller('mainCtrl', function($scope) {
  $scope.form = {};
});

app.controller('CustomerDetailsController', function($scope,$location) {
  $scope.submit = function(){
    if($scope.form1.$valid)
      $location.path('/view2');
  };
});

app.controller('form2Ctrl', function($scope,$location) {
  //this controller contain the data which will you get from
});

Working Plunkr

Update

Clearing confusion of on what form2Ctrl will contain as per request by @user3440121.

The second view may contain a ajax call that will fetch the user information from server and display it on the view or it can be any show the list of employees, Its depend on the whats the requirement of your application. Basically you should not store data on client side as i did stored the data in form object and accessing it on view2 directly as I can access parent scope variable. I shown this only for demonstration purpose. In actual implementation we will take $route parameter from the URL & the we will make an ajax call to server which give data back to you. Currently in plunkr we redirecting to view2 using $location.path('/view2'); that would change to $location.path('/edit/1') and you need to add route to your app.config like

Route

.when('/edit/:id', {
   templateUrl: 'view2.html',
   controller: 'form2Ctrl'
})

Controller

app.controller('form2Ctrl', function($scope,$route) {
   //we can get id here from $route object
   console.log($route.params.id); //here it will be `id` which `1` in the URL
   //now you have id you can make ajax to server and get required data
});

Hope above information cleared all the doubts about the question. Thanks.

Solution 2:

Your $scope.submit function has nothing inside it for routing.

It should be:

$scope.submit = function($scope) { 
  $scope.$apply(function() { 
    $location.path("*place your details page here*"); 
  });
}

Note that you also need to inject $location to your controller, as follows:

FormApp.controller('CustomerDetailsController', 
  ['$scope', '$location', function($scope, $location) ...

Try reading the following for more details: https://docs.angularjs.org/api/ng/service/$location

Also have you tried checking your paths? (e.g. angular.min.js, script.js)

Post a Comment for "Passing Values In Angular Js"