[英]Angular controller not working correctly
I have 3 tabs which should show the corresponding information when clicked: 我有3个标签,在单击时应显示相应的信息:
<section ng-controller="PanelController as panel">
<ul class="nav nav-pills">
<li ng-class="{ active:panel.isSelected(1) }">
<a href ng-click="panel.selectTab(1)">Description</a>
</li>
<li ng-class="{ active:panel.isSelected(2) }">
<a href ng-click="panel.selectTab(2)">Specifications</a>
</li>
<li ng-class="{ active:panel.isSelected(3) }">
<a href ng-click="panel.selectTab(3)">Reviews</a>
</li>
</ul>
{{tab}}
</section>
<div class="panel" ng-show="panel.isSelected(1)">
<h4>Description</h4>
<p>{{product.description}}</p>
</div>
<div class="panel" ng-show="panel.isSelected(2)">
<h4>Specifications</h4>
<blockquote>{{product.specification}}</blockquote>
</div>
<div class="panel" ng-show="panel.isSelected(3)">
<h4>Reviews</h4>
<blockquote>{{product.review}}</blockquote>
</div>
Here is the controller code: 这是控制器代码:
app.controller('PanelController', function(){
this.tab = 1;
this.selectTab = function(setTab) {
this.tab = setTab;
};
this.isSelected = function(checkTab){
return this.tab === checkTab;
};
});
When I click on the tabs, the information does not show (for example; the {{product.description}} does not show my description). 当我单击选项卡时,信息不会显示(例如,{{product.description}}未显示我的描述)。 I have tested the code in developer tools, and it tells me that 'this.tab' in the controller does in-fact change to the corresponding tab number when the different tabs are selected, but I cannot figure out why my information is not showing.
我已经在开发人员工具中测试了代码,它告诉我,当选择了不同的选项卡时,控制器中的'this.tab'实际上会对相应的选项卡号进行了更改,但是我无法弄清楚为什么我的信息没有显示。
Before I added the tabs to my application, the {{product.description}} etc. were working perfectly, however it now does not show when tabs are selected. 在将选项卡添加到应用程序之前,{{product.description}}等已正常运行,但是现在在选择选项卡时不显示。
Few things that are causing that issue 造成该问题的原因很少
You are using as syntax which means that in controller you have to do this: 您正在使用语法,这意味着在控制器中您必须执行以下操作:
app.controller('PanelController', function(){
var panel=this;
panel.tab = 1;
panel.selectTab = function(setTab) {
panel.tab = setTab;
};
panel.isSelected = function(checkTab){
return panel.tab === checkTab;
};
});
And in your html bring ur class panel divs under section tab as they are outside the scope of the controller as per your html like below 并在您的html中将ur类面板的divs置于section选项卡下,因为它们超出了控制器的范围,如下所示
<section ng-controller="PanelController as panel">
<ul class="nav nav-pills">
<li ng-class="{ active:panel.isSelected(1) }">
<a href ng-click="panel.selectTab(1)">Description</a>
</li>
<li ng-class="{ active:panel.isSelected(2) }">
<a href ng-click="panel.selectTab(2)">Specifications</a>
</li>
<li ng-class="{ active:panel.isSelected(3) }">
<a href ng-click="panel.selectTab(3)">Reviews</a>
</li>
</ul>
{{tab}}
<div class="panel" ng-show="panel.isSelected(1)">
<h4>Description</h4>
<p>{{product.description}}</p>
</div>
<div class="panel" ng-show="panel.isSelected(2)">
<h4>Specifications</h4>
<blockquote>{{product.specification}}</blockquote>
</div>
<div class="panel" ng-show="panel.isSelected(3)">
<h4>Reviews</h4>
<blockquote>{{product.review}}</blockquote>
</div>
</section>
You have two problems in your code. 您的代码中有两个问题。
- your panel divs are outside controller div
您的面板div在控制器div外部
- you should use this.panel = 1;
您应该使用this.panel = 1; not this.tab
不是this.tab
var app= angular.module('newApp',[]); app.controller('PanelController', function(){ this.panel = 1; this.selectTab = function(setTab) { this.panel = setTab; }; this.isSelected = function(checkTab){ return this.panel === checkTab; }; });
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script> <div ng-app="newApp"> <section ng-controller="PanelController as panel"> <ul class="nav nav-pills"> <li ng-class="{ active:panel.isSelected(1) }"> <a href ng-click="panel.selectTab(1)">Description</a> </li> <li ng-class="{ active:panel.isSelected(2) }"> <a href ng-click="panel.selectTab(2)">Specifications</a> </li> <li ng-class="{ active:panel.isSelected(3) }"> <a href ng-click="panel.selectTab(3)">Reviews</a> </li> </ul> {{tab}} <div class="panel" ng-show="panel.isSelected(1)"> <h4>Description</h4> <p>{{product.description}}</p> </div> <div class="panel" ng-show="panel.isSelected(2)"> <h4>Specifications</h4> <blockquote>{{product.specification}}</blockquote> </div> <div class="panel" ng-show="panel.isSelected(3)"> <h4>Reviews</h4> <blockquote>{{product.review}}</blockquote> </div> </section> </div>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.