[英]AngularJS - how to get all elements by querySelector and disable links
I'm trying to make a simple step-by-step wizard. 我正在尝试制作一个简单的分步向导。
HTML code for that (in index.html) looks like 该代码的HTML代码(在index.html中)看起来像
<div class="stepwizard col-md-offset-3">
<div class="stepwizard-row setup-panel">
<div class="stepwizard-step">
<a href="/" type="button" class="btn btn-default btn-circle" id="step-1">1</a>
<p>Start</p>
</div>
<div class="stepwizard-step">
<a href="/registration" type="button" class="btn btn-default btn-circle" disabled="disabled" id="step-2">2</a>
<p>Registration</p>
</div>
<div class="stepwizard-step">
<a href="/tutorial" type="button" class="btn btn-default btn-circle" disabled="disabled" id="step-3">3</a>
<p>Tutorial</p>
</div>
<div class="stepwizard-step">
<a href="/thank-you" type="button" class="btn btn-default btn-circle" disabled="disabled" id="step-4">4</a>
<p>Finish</p>
</div>
</div>
</div>
Code above work fine when was triggered by jQuery which looks like function stepnext(n){ 上面的代码在被jQuery触发时工作正常,看起来像stepnext(n){
function step(n){
if(n != 0){
$(".stepwizard-row a").removeClass('btn-primary');
$(".stepwizard-row a").addClass('btn-default');
$('.stepwizard-row #step-'+n+'').removeClass('btn-default');
$('.stepwizard-row #step-'+n+'').addClass('btn-primary');
}
}
but I have moved it to each step controller and triggers it as 但我已经将其移至每个步骤控制器并以
var stepReset = angular.element( document.querySelector( '.stepwizard-row a' ) );
stepReset.removeClass('btn-primary');
stepReset.addClass('btn-default');
var stepActive = angular.element( document.querySelector( '#step-1' ) );
stepActive.removeClass('btn-default');
stepActive.addClass('btn-primary');
and so on for each #step-X
对每个
#step-X
依此类推
It works fine for selecting on each step for btn-primary
but then when I follow to next step like from 2 to 3 is not reseting class from step 2 to btn-default
just taking effect on first element from .stepwizard-row a
which is step#1. 它适合在
btn-primary
每个步骤上进行选择,但是随后当我进行下一步时,例如从2到3时,不会将步骤2的类.stepwizard-row a
为btn-default
只是对.stepwizard-row a
第一个元素生效第1步。
My question is how I can select all DOM elements for .stepwizard-row a
? 我的问题是如何为
.stepwizard-row a
选择所有DOM元素?
Another question is related to have links "disabled" until landed or passed particular step. 另一个问题涉及到链接“禁用”,直到登陆或通过特定步骤。 I have set them to
disabled="disabled"
but AngularJS let me click on them and skip steps. 我将它们设置为
disabled="disabled"
但是AngularJS让我单击它们并跳过步骤。
Manually querying elements from the DOM is rarely needed in AngularJS, and in the rare cases where you need this functionality a directive is more suited. 在AngularJS中,很少需要从DOM中手动查询元素,在极少数情况下,您需要此功能时,可以使用伪指令。
A better way to do this is to use ngClass and add/remove the btn-default
and btn-primary
classes based on an expression, like so: 更好的方法是使用ngClass并基于表达式添加/删除
btn-default
和btn-primary
类,如下所示:
<a href="/" type="button" class="btn btn-circle" ng-class="{'btn-default': notCurrentStep(), 'btn-primary': currentStep()}" id="step-1">1</a>
You can then implement the notCurrentStep
and currentStep
functions in your controller based on your needs. 然后,您可以根据需要在控制器中实现
notCurrentStep
和currentStep
函数。
As for the disabled
attribute and as @Knitesh mentioned, it's better to use ngDisabled so that you can dynamically add/remove the disabled
attribute based on an expression. 至于
disabled
属性和@Knitesh,最好使用ngDisabled,以便您可以基于表达式动态添加/删除disabled
属性。
A similar and easier approach would be : 一种类似且更简单的方法是:
<a href="/" type="button" class="btn btn-circle" ng-class="{'btn-default': step === 1, 'btn-primary': step !== 1 }" id="step-1">1</a>
And in your Controller: 在您的控制器中:
function currentStep (step) { $scope.step = step; }
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.