如何将控制器值从ng-include切换到angularjs? [英] How to switch controller values from ng-include in angularjs?
本文介绍了如何将控制器值从ng-include切换到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 =valueidclass =form-controlrequired typeof =textng-model =valuselectform =Floorform>
< option value =>选择< / 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 =headerng-controller =headerctrlng-include ='header.html'>
< / div>
< div id =sidebarclass =sidebar responsive ace-save-stateng-controller =Contentctrl>
//这个内容页面这里我得到标题下拉列表选择的值
< / div>
< / body>
< / html>
HeaderController
var app = angular.module(app,[]);
app.controller('headerctrl',['$ scope','$ http','$ window',
function($ scope,$ http,$ window,){
}]);
内容控制器
code> var app = angular.module(app,[]);
app.controller('contentctrl',['$ scope','$ http','$ window',
函数($ scope,$ http,$ window,){
}]);
解决方案
您可以使用服务
app.factory('myService',function(){
var savedData = [];
return {
set:set,
get:get
}
函数集(数据){
savedData.push(data)
}
function get() {
return savedData;
}
});
这里是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
这篇关于如何将控制器值从ng-include切换到angularjs?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文