How to switch controller values from ng-include in

2019-03-05 03:11发布

问题:

I am using angularjs. i have one header.html and I have merged that html page into another html using ng-include.

Also, I have one dropdown list in header.html and I want the selected value (from dropdown) list to be displayed. How can I do this?

header.html

<html ng-app="app" > 
  <head>  
      <script src="assets/js/jquery-2.1.4.min.js"></script>
      <script src="controller/headerctrl.js"></script>

  </head> 

<body  ng-controller="headerctrl">

       <select  id="valueid" class="form-control" required  typeof="text" ng-model="valuselect" form="Floorform" >
<option value="">Select</option>  
<option value="1">One</option>  
<option value="2">Two</option>  
<option value="3">Three</option>     
         </select>
    </div>

</body> 
</html>

Content.html

 <html ng-app="app" > 
      <head>  
          <script src="assets/js/jquery-2.1.4.min.js"></script>
          <script src="controller/Contentctrl.js"></script>

      </head> 

    <body  >
    <div id="header" ng-controller="headerctrl" ng-include="'header.html'">
     </div>

<div id="sidebar" class="sidebar responsive ace-save-state" ng-controller="Contentctrl" >

//hi this content page here i get header dropdown selected value

</div>
    </body> 
    </html>

HeaderController

var app = angular.module("app", []);
app.controller('headerctrl', ['$scope', '$http', '$window', 
 function ($scope, $http, $window, ) {

 }]);

content Controller

var app = angular.module("app", []);
    app.controller('contentctrl', ['$scope', '$http', '$window', 
     function ($scope, $http, $window, ) {

     }]);

回答1:

You can use services for it

app.factory('myService', function() {
 var savedData = [];
 return {
  set: set,
  get: get
 }
 function set(data) {
   savedData.push(data)
 }
 function get() {
  return savedData;
 }



});

Here is plnkr

https://plnkr.co/edit/EIqgNJRgeOwY0zDsIDoU?p=preview