JavaScript angularjs using single controller with two views

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" target='_blank' 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>
Answer:1

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)

Answer:2

I have a gulp.task 'scripts' that uses gulp-uglify. Then I also added gulp-import and it didn't work. If i do only uglify it works. If I do only import it works. But if i do both at the same time it ...

I have a gulp.task 'scripts' that uses gulp-uglify. Then I also added gulp-import and it didn't work. If i do only uglify it works. If I do only import it works. But if i do both at the same time it ...

  1. include gulp in package.json

I am working on Google Spreadsheet's project where I am adding/deleting TimeBased triggers through scripting. All triggers fired will run one function, which will check its Id and run function ...

I am working on Google Spreadsheet's project where I am adding/deleting TimeBased triggers through scripting. All triggers fired will run one function, which will check its Id and run function ...

  1. google script trigger on form submit
  2. google script trigger not working
  3. google script trigger on change
  4. google script trigger limit
  5. google script trigger quota
  6. google script trigger time
  7. google script trigger onedit
  8. google script trigger
  9. google script trigger tutorial
  10. google script trigger installable
  11. google apps script trigger programmatically
  12. google apps script trigger
  13. google apps script trigger on form submit
  14. google sheets trigger script
  15. google script email trigger
  16. google script create trigger
  17. google apps script trigger edit
  18. google apps script trigger seconds
  19. google apps script trigger source
  20. google script get trigger

I'm embedding Rhino in Java and I want to create a global property with a getter and setter. This non-global property definition works: var testObj = {}; Object.defineProperty(testObj, 'testPropName',...

I'm embedding Rhino in Java and I want to create a global property with a getter and setter. This non-global property definition works: var testObj = {}; Object.defineProperty(testObj, 'testPropName',...

  1. define global property
  2. gradle define global property
  3. soapui define global property
  4. ant define global property
  5. maven define global properties

I need to plot a graph in a canvas. But how can I use an algebra equation as input, and based on the equation, draw the curve, using javascript? For example: x2+5y=250 The equation plots a graph ...

I need to plot a graph in a canvas. But how can I use an algebra equation as input, and based on the equation, draw the curve, using javascript? For example: x2+5y=250 The equation plots a graph ...

  1. plot graph based on points
  2. plot graph based on equation
  3. plot graph based on equation excel
  4. plot based graph
  5. plot the graph based on your observations
  6. plot the graph based on multicollinearity