本质上,我试图让按钮在div上设置ng-class
。按钮是使用ng-repeat
从阵列创建的,阵列将随着时间的推移而增长。我根据我的想法编写了this page of the AngularJS documentation的最后一个示例,该示例没有使用ng-click
的函数。我是AngularJS的新手,仍然试图摆脱我的jQuery思维模式,所以让我知道这是不是最好的方法去做这件事。用AngularJS ngClick和ngClass改变类的按钮
的jsfiddle:http://jsfiddle.net/E2GB9/ - 不知道为什么它不渲染引导100%,但它不应该的问题..
当我使用的开发工具来检查ng-click
后加载它看起来是正确的:ng-click="appliedStyle='example1'"
但点击按钮的功能没有设置appliedStyle。
HTML:
<body ng-app>
<div ng-controller="TypeCtrl">
<div class="tabbable">
<ul class="nav nav-tabs">
<li class="active"><a href="#source" data-toggle="tab">Test Area</a></li>
</ul>
<div class="tab-content">
<div class="tab-pane active" id="source">
<div class="hero-unit" ng-class="appliedStyle">
<h1>H1 Header</h1>
<h2>H2 Header</h2>
<p>
Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod
tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam,
quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo
consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse
cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non
proident, sunt in culpa qui officia deserunt mollit anim id est laborum.
</p>
</div>
</div>
</div>
</div>
<ul class="unstyled">
<li ng-repeat="style in styles">
<span>Name: {{style.name}}</span><br>
<span>H1: {{style.h1}}</span><br>
<span>H2: {{style.h2}}</span><br>
<span>P: {{style.p}}</span><br>
<button class="btn-small" type="submit" ng-click="appliedStyle='{{style.className}}'">Apply {{style.name}}</button>
</li>
</ul>
The Applied Style is : {{appliedStyle}}
</div>
JS:
function TypeCtrl($scope) {
$scope.styles = [
{name: 'Example 1', h1:'32px', h2:'24px', p:'12px', className:'example1'}];
}
CSS:
.example1 h1{
font-size: 10px;
color: red;
}
.example1 h2{
font-size: 8px;
}
.example1 p{
font-size: 6px;
}
顺便说一句,“应用风格是:{{appliedStyle}}'不在控制器之外。把它放到'div' –
@MaximShoustin忘了我把它留在那里,直到我读到这个,并且像“哦,哦”这样的混淆。谢谢! – tehaaron