Hi I want same controller in different pages. How to pass the object to different pages. Below is my sample code.
Page1.html
<body ng-app="myapp" ng-controller="studentController">
<div>
<table border="0">
<tr>
<td>
<table>
<tr>
<th>Name</th>.
<th>Marks</th>
</tr>
<tr ng-repeat="subject in student.subjects">
<td> <a href="#Page2">{{ subject.name }}</a></td>
<td>{{ subject.marks }}</td>
</tr>
</table>
</td>
</tr>
</table>
</div>
<div ng-view></div>
</body>
Script.js
var app = angular.module("myapp", ["ngRoute"]);
app.config(function ($routeProvider) {
$routeProvider
.when("/Page1", {
template: '<h1>Page 1</h1>'
})
.when("/Page2", {
templateUrl: 'Page2.html',
controller: 'studentController'
})
.otherwise({
template: '<h1>note found</h1>'
})
});
app.controller('studentController', function ($scope) {
$scope.student = {
firstName: "AAA",
lastName: "ZZZ",
fees: 500,
subjects: [
{ name: 'Physics', marks: 70 },
{ name: 'Chemistry', marks: 80 },
{ name: 'Math', marks: 65 },
{ name: 'English', marks: 75 },
{ name: 'Computers', marks: 67 }
],
};
});
Page2.html
<body ng-app="myapp" ng-controller="studentController">
<h1>Hello</h1>
<h2>
subject name - {{ subject.name }}
</h2>
</body>
How to pass subject object from page 1 to page 2.
I define same controller in route config.
Is there anything do i need to define?
You can use a service for that. A service is a singleton in AngularJs, so there is only one instance of it, which makes it a perfect fit if you wanna share data over different controllers
You can then inject it in your controller(s):