- 首页
- 前端开发
- 如何使用 angular 和 UI 引导程序将动态内容添加到选项卡集
如何使用 angular 和 UI 引导程序将动态内容添加到选项卡集
[英] How to add dynamic contents to tab set using angular and UI bootstrap
本文介绍了如何使用 angular 和 UI 引导程序将动态内容添加到选项卡集的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
angular 和 ui 引导程序非常新.我有一个标签集如下:
<meta http-equiv="X-UA-Compatible" content="IE=edge"/><标题>标签<link href="css/bootstrap.css" rel="样式表">头部><身体><form id="formTabs" runat="server"><div><div ng-controller="TabsDemoCtrl"><小时/><tabset align="left"><tab header="Account">账户内容</tab><tab header="Policy">政策内容</tab><tab header="LoB">LOB 内容</tab><tab header="Coverage">Coverage content</tab><tab header="详细损失">详细损失</tab></tabset>
</表单>
javascript 为:
angular.module('ui.bootstrap.demo', ['ui.bootstrap']);angular.module('ui.bootstrap.demo').controller('TabsDemoCtrl', function($scope) {});
现在我想增强上面的例子,让 Tab 的内容是动态的.我需要通过调用 Web 服务来获取数据.假设当我单击帐户"选项卡时,将调用函数 LoadProducts 并加载数据.
我将更改 javascript 以包含函数 LoadProducts,如下所示:
angular.module('ui.bootstrap.demo', ['ui.bootstrap']);angular.module('ui.bootstrap.demo').controller('TabsDemoCtrl', function($scope) {$scope.LoadProducts = function() {$scope.loading = true;var PnrUrlLoadProducts = PNRfolder +'/Portal/WebServices/PNRServices.asmx/getPNRProducts';$http({方法:'POST',//url: '/Portal/WebServices/PNRServices.asmx/getPNRProducts',网址:PnrUrlLoadProducts,数据:{'businessUnit':$scope.businessUnit.desc},标头:{'内容类型':'应用程序/json;charset=utf-8', 'dataType': 'json' },缓存:$templateCache}).成功(功能(数据,状态,标题,配置){......}).
现在我不知道如何通过单击帐户"选项卡来调用 Loadproducts.拜托,有人可以帮忙吗?
我更改了代码以包含 ng-click 的功能.这不起作用.请参阅以下内容:
<头><meta http-equiv="X-UA-Compatible" content="IE=edge"/><标题>标签<link href="css/bootstrap.css" rel="stylesheet"/>头部><身体><form id="formTabs" runat="server"><div><div ng-controller="TabsDemoCtrl"><小时/><tabset align="left"><tab header="Account" ng-click ="ShowTest">Account content</tab><tab header="Policy">政策内容</tab><tab header="LoB">LOB 内容</tab><tab header="Coverage">Coverage content</tab><tab header="详细损失">详细损失</tab></tabset>
</表单></html>
javascript:
angular.module('ui.bootstrap.demo', ['ui.bootstrap']);angular.module('ui.bootstrap.demo').controller('TabsDemoCtrl', function($scope) {$scope.ShowTest = function() {$scope.loading = true;警报(在帐户中");}
});
当我单击帐户"选项卡时没有任何反应.它仍然显示静态文本.
******** 重新访问我的代码 ****************我确实更改了我的代码,如下所示,但仍然在单击帐户选项卡时未调用函数 ShowTest.
HTML 是:
<头><meta http-equiv="X-UA-Compatible" content="IE=edge"/><标题>标签<link href="css/bs/bootstrap.css" rel="stylesheet"/><style type="text/css" >.SpanStyle{文本对齐:居中;颜色:蓝色;}</风格>头部><身体><form id="formTabs" runat="server"><div><div ng-controller="TabsDataCtrl"><小时/><tabset align="left"><tab header="Account" ng-click ="ShowTest">账户内容<div id ="TestAcccount"><span class="SpanStyle">测试</span></div></tab><tab header="Policy">政策内容</tab><tab header="LoB">LOB 内容</tab><tab header="Coverage">Coverage content</tab><tab header="详细损失">详细损失</tab></tabset>
</表单></html><script src="jsNew/angular.js" type ="text/javascript"></script>
javascript 如下:
angular.module('ui.bootstrap.testData', ['ui.bootstrap']);angular.module('ui.bootstrap.testData').controller('TabsDataCtrl', function($scope) {警报(模块");$scope.ShowTest = function() {警报(范围");$scope.loading = true;var PnrUrlBU = PNRfolder + '/Portal/WebServices/PNRServices.asmx/getPNRBusinessUnits';$http({方法:'POST',//url: '/Portal/WebServices/PNRServices.asmx/getPNRBusinessUnits',网址:PnrUrlBU,数据: {},标头:{'内容类型':'应用程序/json;字符集=uf-8','数据类型':'json' }}).成功(功能(数据,状态,标题,配置){$scope.loading = false;//$scope.businessUnits = data.d;//$("#ddBusinessUnits").removeAttr("disabled");//$scope.GetPNRHistory();$("#TestAcccount span.SpanStyle").text("业务单位:" + data.d);$scope("#TestAcccount").show();}).错误(功能(数据,状态){$scope.loading = false;alert("请求失败的GetBusinessUnits");});}});
显示第一个警报模块",但不显示第二个警报范围".这意味着函数 ShowTest 永远不会被触发.
解决方案
您将 jQuery 和 Angular 混合在一起,这会导致您遇到问题.Angular 有另一种显示和隐藏方式,你应该在整个过程中使用它.我已经玩过你的 Plunkr,但你现在需要测试它,因为 $http 调用需要一个完整的 URL 来 POST 到,但我能够让错误警报工作,所以在严格的情况下也应该成功
angular.module('ui.bootstrap.demo', ['ui.bootstrap']);angular.module('ui.bootstrap.demo').controller('TabsDemoCtrl', function($scope) {$scope.LoadProducts = function() {$scope.loading = true;var PnrUrlLoadProducts = PNRfolder + '/Portal/WebServices/PNRServices.asmx/getPNRProducts';$http({方法:'POST',//可能应该是一个GET网址:PnrUrlLoadProducts,数据:{'businessUnit':$scope.businessUnit.desc},标头:{'内容类型':'应用程序/json;charset=utf-8', 'dataType': 'json' },缓存:$templateCache}).成功(功能(数据,状态,标题,配置){$scope.loading = false;警报(在帐户中");...})}})
有关所有详细信息,请参阅更新的 plnkr.
Very new to angular and ui bootstrap. I have a tab set as below:
<head>
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<title> Tabs</title>
<link href="css/bootstrap.css" rel="stylesheet">
</head>
<body>
<form id="formTabs" runat="server">
<div>
<div ng-controller="TabsDemoCtrl">
<hr />
<tabset align="left">
<tab heading="Account">Account content</tab>
<tab heading="Policy">Policy content</tab>
<tab heading="LoB">LOB content</tab>
<tab heading="Coverage">Coverage content</tab>
<tab heading="Detailed Loss">Detailed Loss</tab>
</tabset>
</div>
</div>
</form>
The javascript as :
angular.module('ui.bootstrap.demo', ['ui.bootstrap']);
angular.module('ui.bootstrap.demo').controller('TabsDemoCtrl', function($scope) {
});
Now I want to enhance the above example so that the contents of Tab are dynamic. I need to get data by invoking web service. say when I click Account Tab, function LoadProducts is called and the data is loaded.
I would change the javascript to include the function LoadProducts as below:
angular.module('ui.bootstrap.demo', ['ui.bootstrap']);
angular.module('ui.bootstrap.demo').controller('TabsDemoCtrl', function($scope) {
$scope.LoadProducts = function() {
$scope.loading = true;
var PnrUrlLoadProducts = PNRfolder +
'/Portal/WebServices/PNRServices.asmx/getPNRProducts';
$http(
{
method: 'POST',
//url: '/Portal/WebServices/PNRServices.asmx/getPNRProducts',
url: PnrUrlLoadProducts,
data: { 'businessUnit': $scope.businessUnit.desc },
headers: { 'Content-Type': 'application/json; charset=utf-8', 'dataType': 'json' },
cache: $templateCache
}).
success(function(data, status, headers, config) {
...
...
}).
Now I do not know how to call the Loadproducts on click of the tab "Accounts". Please, can someone help?
I changed the code to include the fuction on ng-click. This does not work . Please see below:
<html ng-app="ui.bootstrap.demo">
<head>
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<title> Tabs</title>
<link href="css/bootstrap.css" rel="stylesheet"/>
</head>
<body>
<form id="formTabs" runat="server">
<div>
<div ng-controller="TabsDemoCtrl">
<hr />
<tabset align="left">
<tab heading="Account" ng-click ="ShowTest">Account content</tab>
<tab heading="Policy">Policy content</tab>
<tab heading="LoB">LOB content</tab>
<tab heading="Coverage">Coverage content</tab>
<tab heading="Detailed Loss">Detailed Loss</tab>
</tabset>
</div>
</div>
</form>
</body>
</html>
javascript:
angular.module('ui.bootstrap.demo', ['ui.bootstrap']);
angular.module('ui.bootstrap.demo').controller('TabsDemoCtrl', function($scope) {
$scope.ShowTest = function() {
$scope.loading = true;
alert("in accounts");
}
});
Nothing happens when I click Account tab. It still shows static text.
******** Revisited my code ***************
I did change my code as below, and still, on click of accounts tab, the function ShowTest is not called.
The HTML is :
<html ng-app="ui.bootstrap.testData">
<head>
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<title> Tabs</title>
<link href="css/bs/bootstrap.css" rel="stylesheet"/>
<style type ="text/css" >
.SpanStyle
{
text-align: center;
color:blue;
}
</style>
</head>
<body>
<form id="formTabs" runat="server">
<div>
<div ng-controller="TabsDataCtrl">
<hr />
<tabset align="left">
<tab heading="Account" ng-click ="ShowTest">Account content
<div id ="TestAcccount"> <span class="SpanStyle">Test</span></div>
</tab>
<tab heading="Policy">Policy content</tab>
<tab heading="LoB">LOB content</tab>
<tab heading="Coverage">Coverage content</tab>
<tab heading="Detailed Loss">Detailed Loss</tab>
</tabset>
</div>
</div>
</form>
</body>
</html>
<script src="jsNew/angular.js" type ="text/javascript"></script>
<script type ="text/javascript" >
$(window).load(function() {
$("#TestAcccount").hide();
});
</script>
The javascript is as :
angular.module('ui.bootstrap.testData', ['ui.bootstrap']);
angular.module('ui.bootstrap.testData').controller('TabsDataCtrl', function($scope) {
alert("module");
$scope.ShowTest = function() {
alert("scope");
$scope.loading = true;
var PnrUrlBU = PNRfolder + '/Portal/WebServices/PNRServices.asmx/getPNRBusinessUnits';
$http({
method: 'POST',
//url: '/Portal/WebServices/PNRServices.asmx/getPNRBusinessUnits',
url: PnrUrlBU,
data: {},
headers: { 'Content-Type': 'application/json; charset=uf-8', 'dataType': 'json' }
}).
success(function(data, status, headers, config) {
$scope.loading = false;
//$scope.businessUnits = data.d;
//$("#ddBusinessUnits").removeAttr("disabled");
// $scope.GetPNRHistory();
$("#TestAcccount span.SpanStyle").text("The business unit:" + data.d);
$scope("#TestAcccount").show();
}).
error(function(data, status) {
$scope.loading = false;
alert("Request Failed GetBusinessUnits");
});
}
});
The first alert "module " is displayed but the second alert "scope" is not displayed. This implies the function ShowTest is never fired.
解决方案
You are mixing jQuery and Angular up in a way that will lead you into problems. Angular has another way of doing showing and hiding and you should use that throughout. I've played around with your Plunkr but you'll now need to test it as the $http call needs a full URL to POST to, but I was able to get the error alert to work so success should fire too under the rtight circumstances
angular.module('ui.bootstrap.demo', ['ui.bootstrap']);
angular.module('ui.bootstrap.demo')
.controller('TabsDemoCtrl', function($scope) {
$scope.LoadProducts = function() {
$scope.loading = true;
var PnrUrlLoadProducts = PNRfolder + '/Portal/WebServices/PNRServices.asmx/getPNRProducts';
$http(
{
method: 'POST', // probably should be a GET
url: PnrUrlLoadProducts,
data: { 'businessUnit': $scope.businessUnit.desc },
headers: { 'Content-Type': 'application/json; charset=utf-8', 'dataType': 'json' },
cache: $templateCache
}).
success(function(data, status, headers, config) {
$scope.loading = false;
alert("in accounts");
...
})
}
})
See updated plnkr for all the details.
这篇关于如何使用 angular 和 UI 引导程序将动态内容添加到选项卡集的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文