在AngularJS中,要通过一个a标签简单地显示一个字段,我会这样做:
直到这里都没有问题。我现在想添加更多的按钮和字段,以便当我点击A时,显示A的内容,然后当我点击按钮B时,A的内容消失,B的内容出现。
我该怎么做?谢谢。
更新
谢谢大家提供的解决方案,它们有效!现在,我正在为每个和创建一个模板,因为我有很多数据要显示,但所有数据结构都相同。
这是index.html。
<div ng-show="aField">Content of aField</div>
<a ng-click="aField=true">Show aField</a>
直到这里都没有问题。我现在想添加更多的按钮和字段,以便当我点击A时,显示A的内容,然后当我点击按钮B时,A的内容消失,B的内容出现。
我该怎么做?谢谢。
更新
谢谢大家提供的解决方案,它们有效!现在,我正在为每个和创建一个模板,因为我有很多数据要显示,但所有数据结构都相同。
这是index.html。
<div ng-model="methods"
ng-include="'templateMethod.html'"
ng-repeat = "method in methods">
这里是script.js:
function Ctrl($scope) {
$scope.methods =
[ { name: 'method1',
description: 'bla bla bla',
benefits: 'benefits of method1',
bestPractices : 'bestPractices',
example: 'example'},
{ name: 'method2',
description: 'bla bla bla',
benefits: 'benefits of method2',
bestPractices : 'bestPractices',
example: 'example'} ];
}
以下是templateMethod.html:
<table>
<tr>
<td>
<div ng-show="toShow=='{{method.name}}Field'">
<h3>{{mmethodethod.name}}</h3>
<p>
<strong>Description</strong>
{{method.description}}
</p>
<p>
<strong>Benefits</strong>
{{method.benefits}}
</p>
<p>
<strong>Best practices</strong>
{{method.bestPractices}}
</p>
<p>
<strong>Examples</strong>
{{method.example}}
</p>
</div>
</td>
<td class = "sidebar">
<ul>
<li><a ng-click="toShow='{{method.name}}Field'" class="{{method.name}} buttons">{{method.name}}</a></li>
</ul>
</td>
</tr>
</table>
它起作用了! 但是:如果我先点击第一个按钮,然后再点击第二个按钮,第一个按钮的内容不会消失,而是出现在第一个按钮的内容下面... 重复的问题?
谢谢