【问题标题】:How to switch controller values from ng-include in angularjs?如何在angularjs中从ng-include切换控制器值?
【发布时间】:2017-09-27 10:08:13
【问题描述】:

我正在使用 angularjs。我有一个header.html,我已经使用 ng-include 将该 html 页面合并到另一个 html 中。

另外,我在header.html 中有一个下拉列表,我希望显示所选值(来自下拉列表)列表。我该怎么做?

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>

内容.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>

标头控制器

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

 }]);

内容控制器

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

     }]);

【问题讨论】:

  • 为什么在 header 中使用 ng-app
  • 我的模块名称
  • 我会尝试创建 plnkr 。你应该阅读github.com/johnpapa/angular-styleguide/tree/master/a1。这是非常好的角度指南
  • @ThanhTùng 在html 元素中使用ng-app 很好
  • 请检查我会更新代码@ThanhTùng @k Scaandrett

标签: angularjs drop-down-menu controller mean-stack


【解决方案1】:

你可以为它使用服务

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



});

这里是 plnkr

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-06-15
    • 1970-01-01
    相关资源
    最近更新 更多