如何从angularjs中的ng-include切换控制器值? [英] How to switch controller values from ng-include in angularjs?

查看:23
本文介绍了如何从angularjs中的ng-include切换控制器值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

header.html

<头><script src="assets/js/jquery-2.1.4.min.js"></script><script src="controller/headerctrl.js"></script><body ng-controller="headerctrl"><select id="valueid" class="form-control" required typeof="text" ng-model="valueselect" form="Floorform" ><option value="">选择</option><option value="1">一个</option><option value="2">两个</option><option value="3">三</option></选择>

内容.html

 <头><script src="assets/js/jquery-2.1.4.min.js"></script><script src="controller/Contentctrl.js"></script><身体><div id="header" ng-controller="headerctrl" ng-include="'header.html'">

<div id="sidebar" class="sidebar 响应式ace-save-state" ng-controller="Contentctrl" >//你好这个内容页面在这里我得到标题下拉选择值

HeaderController

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

内容控制器

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

解决方案

你可以使用它的服务

app.factory('myService', function() {var savedData = [];返回 {设置:设置,得到:得到}功能集(数据){savedData.push(data)}函数获取(){返回保存的数据;}});

这里是 plnkr

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

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, ) {

     }]);

解决方案

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

这篇关于如何从angularjs中的ng-include切换控制器值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
其他开发最新文章
热门教程
热门工具
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆