How to switch controller values from ng-include in angularjs?

不羁岁月 提交于 2019-12-02 18:55:05

问题


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



来源:https://stackoverflow.com/questions/43694605/how-to-switch-controller-values-from-ng-include-in-angularjs

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!