angularjs using single controller with two views

Haider

So I have this situation where I have two views. One view takes several inputs from the user, the second view shows the results of the inputs after performing some calculations. Since both the views fall under different divs I have to define same ng-controller for both of them separately.

Now the problem is that the changes in the input view are not being reflected in the output view.

var app = angular.module("app", []);
app.controller('controller',function ($scope) {
  $scope.val1 = 10;
  $scope.val2 = 0;
  $scope.val3 = 0;
  $scope.val4 = 0;
  $scope.cal = function() {
    return parseInt($scope.val1) + parseInt($scope.val2) + parseInt($scope.val3) + parseInt($scope.val4);
  }
});
<!DOCTYPE html>
<html>

  <head><script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
    <link rel="stylesheet" href="style.css" />
    <script src="script.js"></script>
  </head>

  <body ng-app="app">
    <form action="#" ng-controller="controller">
  		<input type="text" ng-model="val1"><br>
  		<input type="text" ng-model="val2"><br>
  		<input type="text" ng-model="val3"><br>
  		<input type="text" ng-model="val4"><br>
  		Self:{{cal()}}
  	</form>
  
  	<div ng-controller="controller">
  	  Other:{{cal()}}
  	</div>
  </body>

</html>

Now I can make it work by redefining all the val1, val2, val3 and val4 inputs as hidden in the output view but that is an ugly solution with redundant code. What is the correct way of doing it.

Update: My both the views are far apart from each other and a lot of code lives between them both. I don't want to find a common ancestor div and assign it the controller as that will nest other controllers which are associated with the views in between them. This will complicate my situation.

AndrewP

A quick search for this found another SO question with explaination and answer.

Each time the Angular compiler finds ng-controller in the HTML, a new scope is created. (If you use ng-view, each time you go to a different route, a new scope is created too.)

If you need to share data between controllers, normally a service is your best option. Put the shared data into the service, and inject the service into the controlle

Using the same controller on different elements to refer to the same object

(not my response - please upvote that answer if it helps you out)

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Responsibility of a controller in AngularJS - More than two views?

From Dev

How to redirect views using the same angularjs controller?

From Dev

Can I use one controller updating two views in AngularJS?

From Dev

Controller in Nested views of AngularJS

From Dev

Angularjs controller for multiple views

From Dev

Angularjs controller for multiple views

From Dev

Controller in Nested views of AngularJS

From Dev

Using same controller in two places in AngularJS

From Dev

Using Two Controllers in a Single AngularJS App

From Dev

Using Two Controllers in a Single AngularJS App

From Dev

multiple views in a single state sharing a single controller using ui-router

From Dev

UI-Router Multiple Views Single Controller

From Dev

Using model in views / controller in codeigniter

From Dev

AngularJs - Broadcast event to single controller

From Dev

Controlling multiple views in one controller in AngularJS

From Dev

Controlling multiple views in one controller in AngularJS

From Dev

AngularJS Directive or Controller to seperate views components

From Dev

AngularJS Multiple Dynamic Views based on Single Route

From Dev

Two table views in view controller: Swift

From Dev

How to set two views in a controller in Zend framework

From Dev

Two table views in view controller: Swift

From Dev

Adding two container views to a view controller

From Dev

How to use a single JSON object in two controller without seperate call in angularjs?

From Dev

Using Two Views and switch between Views

From Dev

How can I place two views in a single screen using autolayout Constraints

From Dev

How to combine two similar views into a single response?

From Dev

How to share single Viewmodel in two or more views?

From Dev

Using AngularJS with a factory and a Controller

From Dev

Using a AngularJS Factory in a Controller

Related Related

  1. 1

    Responsibility of a controller in AngularJS - More than two views?

  2. 2

    How to redirect views using the same angularjs controller?

  3. 3

    Can I use one controller updating two views in AngularJS?

  4. 4

    Controller in Nested views of AngularJS

  5. 5

    Angularjs controller for multiple views

  6. 6

    Angularjs controller for multiple views

  7. 7

    Controller in Nested views of AngularJS

  8. 8

    Using same controller in two places in AngularJS

  9. 9

    Using Two Controllers in a Single AngularJS App

  10. 10

    Using Two Controllers in a Single AngularJS App

  11. 11

    multiple views in a single state sharing a single controller using ui-router

  12. 12

    UI-Router Multiple Views Single Controller

  13. 13

    Using model in views / controller in codeigniter

  14. 14

    AngularJs - Broadcast event to single controller

  15. 15

    Controlling multiple views in one controller in AngularJS

  16. 16

    Controlling multiple views in one controller in AngularJS

  17. 17

    AngularJS Directive or Controller to seperate views components

  18. 18

    AngularJS Multiple Dynamic Views based on Single Route

  19. 19

    Two table views in view controller: Swift

  20. 20

    How to set two views in a controller in Zend framework

  21. 21

    Two table views in view controller: Swift

  22. 22

    Adding two container views to a view controller

  23. 23

    How to use a single JSON object in two controller without seperate call in angularjs?

  24. 24

    Using Two Views and switch between Views

  25. 25

    How can I place two views in a single screen using autolayout Constraints

  26. 26

    How to combine two similar views into a single response?

  27. 27

    How to share single Viewmodel in two or more views?

  28. 28

    Using AngularJS with a factory and a Controller

  29. 29

    Using a AngularJS Factory in a Controller

HotTag

Archive