angularjs形成向导验证

时间:2015-08-20 22:59:21

标签: javascript angularjs validation formwizard

所以我依赖于使用 <?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:ads="http://schemas.android.com/apk/res-auto" android:layout_width="match_parent" android:layout_height="match_parent" android:background="#e8e8e8" android:orientation="vertical"> <android.support.v7.widget.Toolbar xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/toolbarMainScreen" android:layout_width="match_parent" android:layout_height="@dimen/visinaHeadera" android:background="#1d6cb5" android:minHeight="?attr/actionBarSize" android:theme="@style/Widget.AppCompat.Toolbar"> <ImageButton android:layout_width="wrap_content" android:layout_height="match_parent" android:layout_gravity="left" android:background="@drawable/senkaokoikonica" android:scaleType="center" android:src="@drawable/homeicon" /> <LinearLayout android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="horizontal"> <TextView android:layout_width="0dp" android:layout_height="match_parent" android:layout_marginLeft="@dimen/razmakOdHomeDoTexta" android:layout_weight="1" android:paddingTop="@dimen/marginaTextaZaIspisuHeaderu" android:text="Akcije i katalozi" android:textAlignment="center" android:textColor="#ffffff" android:textSize="15sp" android:typeface="sans" /> <ImageButton android:id="@+id/imageButtonOtvoriSearch" android:layout_width="wrap_content" android:layout_height="match_parent" android:background="@drawable/search_klik" android:scaleType="center" android:src="@drawable/searchicon" /> <View android:layout_width="@dimen/razmakIzmedjuLupeIShare" android:layout_height="match_parent" /> <ImageButton android:id="@+id/shareButtonKataloziMainScreen" android:layout_width="wrap_content" android:layout_height="match_parent" android:background="@drawable/senkaokoikonica" android:scaleType="center" android:src="@drawable/shareicon" /> <View android:layout_width="@dimen/razmakIzmedjuLupeIShare" android:layout_height="match_parent" /> <ImageButton android:id="@+id/settingsButtonMainScreen" android:layout_width="wrap_content" android:layout_height="match_parent" android:background="@drawable/senkaokoikonica" android:scaleType="center" android:src="@drawable/settingsicon" /> <View android:layout_width="@dimen/razmakIzmedjuSettingsIDesnogKraja" android:layout_height="match_parent" /> </LinearLayout> </android.support.v7.widget.Toolbar> <LinearLayout android:layout_width="match_parent" android:layout_height="3.33dp" android:layout_below="@+id/toolbarMainScreen" android:background="@drawable/headshad480" /> <LinearLayout android:layout_width="match_parent" android:layout_height="3.33dp" android:layout_below="@+id/linearLayoutZaSearchMainScreen" android:background="@drawable/headshad480" android:visibility="invisible" /> <LinearLayout android:id="@+id/linearLayoutZaSearchMainScreen" android:layout_width="match_parent" android:layout_height="@dimen/visinaDrugogHeadera" android:layout_below="@+id/toolbarMainScreen" android:background="@drawable/searchbg" android:orientation="horizontal" android:visibility="invisible"> <LinearLayout android:layout_width="0dp" android:layout_height="match_parent" android:layout_weight="1" android:background="@drawable/searchbg"> <EditText android:id="@+id/editTextMainScreen" android:layout_width="match_parent" android:layout_height="@dimen/editTextSearchVisina" android:layout_marginBottom="@dimen/editTextMarginaBot" android:layout_marginLeft="@dimen/editTextMarginaLeft" android:layout_marginRight="@dimen/editTextMarginaRight" android:layout_marginTop="@dimen/editTextMarginaTop" android:layout_weight="1" android:background="@drawable/edittext_conrer" android:hint="Pretraga" android:textColor="#000000" android:textColorHint="#000000" android:textStyle="italic" /> </LinearLayout> <ImageButton android:id="@+id/imageButtonSearch" android:layout_width="wrap_content" android:layout_height="@dimen/searchButtSearchVisina" android:layout_gravity="right" android:layout_marginBottom="@dimen/searchButtMarginaBot" android:layout_marginRight="@dimen/searchButtMarginaRight" android:layout_marginTop="@dimen/searchButtMarginaTop" android:background="@drawable/search_klik" android:scaleType="center" android:src="@drawable/searchiconkrug" /> </LinearLayout> <LinearLayout android:layout_width="match_parent" android:layout_height="0dp" android:layout_above="@+id/linearLayoutZaReklameMainScreen" android:layout_alignParentEnd="true" android:layout_alignParentRight="true" android:layout_below="@+id/toolbarMainScreen" android:layout_weight="1" android:baselineAligned="false" android:orientation="vertical"> <LinearLayout android:layout_width="match_parent" android:layout_height="0dp" android:layout_weight="1"> <LinearLayout android:layout_width="0dp" android:layout_height="match_parent" android:layout_weight="1" android:orientation="vertical"> <ImageButton android:id="@+id/buttonProdavci" android:layout_width="match_parent" android:layout_height="0dp" android:layout_marginBottom="@dimen/marginIzmedjuSlika" android:layout_marginLeft="@dimen/marginIzmedjuSlikaIZida" android:layout_marginRight="@dimen/marginIzmedjuSlika" android:layout_marginTop="@dimen/marginTopZaGornjeSlike" android:layout_weight="1" android:background="@drawable/prodavci" android:scaleType="center" /> </LinearLayout> <LinearLayout android:layout_width="0dp" android:layout_height="match_parent" android:layout_weight="1" android:orientation="vertical"> <ImageButton android:id="@+id/buttonProizvodi" android:layout_width="match_parent" android:layout_height="0dp" android:layout_marginBottom="@dimen/marginIzmedjuSlika" android:layout_marginLeft="@dimen/marginIzmedjuSlika" android:layout_marginRight="@dimen/marginIzmedjuSlikaIZida" android:layout_marginTop="@dimen/marginTopZaGornjeSlike" android:layout_weight="1" android:background="@drawable/proizvodi" android:scaleType="center" /> </LinearLayout> </LinearLayout> <LinearLayout android:layout_width="match_parent" android:layout_height="0dp" android:layout_weight="1"> <LinearLayout android:layout_width="0dp" android:layout_height="match_parent" android:layout_weight="1" android:orientation="vertical"> <ImageButton android:id="@+id/buttonBarkod" android:layout_width="match_parent" android:layout_height="0dp" android:layout_marginBottom="@dimen/marginIzmedjuSlikaIZida" android:layout_marginLeft="@dimen/marginIzmedjuSlikaIZida" android:layout_marginRight="@dimen/marginIzmedjuSlika" android:layout_marginTop="@dimen/marginIzmedjuSlika" android:layout_weight="1" android:adjustViewBounds="true" android:background="@drawable/barcode" android:scaleType="center" /> </LinearLayout> <LinearLayout android:layout_width="0dp" android:layout_height="match_parent" android:layout_weight="1" android:orientation="vertical"> <ImageButton android:id="@+id/buttonSviKatalozi" android:layout_width="match_parent" android:layout_height="0dp" android:layout_centerHorizontal="true" android:layout_marginBottom="@dimen/marginIzmedjuSlikaIZida" android:layout_marginLeft="@dimen/marginIzmedjuSlika" android:layout_marginRight="@dimen/marginIzmedjuSlikaIZida" android:layout_marginTop="@dimen/marginIzmedjuSlika" android:layout_weight="1" android:adjustViewBounds="true" android:background="@drawable/svikatalozi" android:scaleType="center" /> </LinearLayout> </LinearLayout> </LinearLayout> <LinearLayout android:id="@+id/linearLayoutZaReklameMainScreen" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_alignParentBottom="true" android:gravity="center|bottom"> <com.google.android.gms.ads.AdView android:id="@+id/adViewMainScreen" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentBottom="true" android:layout_centerHorizontal="true" android:layout_gravity="bottom|center" ads:adSize="BANNER" ads:adUnitId="@string/aik_ad_unit_id"></com.google.android.gms.ads.AdView> </LinearLayout> <LinearLayout android:id="@+id/lejoutZaTransparentonstMainScreen" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_alignParentBottom="true" android:layout_below="@id/toolbarMainScreen" android:alpha=".35" android:background="#000000" android:orientation="horizontal" android:visibility="invisible" /> </RelativeLayout> 指令的下一步的链接。但我发现除了当前步骤之外的其他步骤的另一个链接是ng-form

所以我改为使用enabled的标志。这是我的代码:

的index.html

ng-disabled

app.js

<div class="stepwizard-row setup-panel">
    <div class="stepwizard-step">
        <a ui-sref="step1" type="button" class="btn btn-primary btn-circle active-step-wizard">1</a>
    </div>
    <div class="stepwizard-step">
        <a ng-disabled="step1Form.$invalid;" ui-sref="step2" type="button" class="btn btn-primary btn-circle active-step-wizard">2</a>
    </div>
    <div class="stepwizard-step">
        <a ng-disabled="step2Disabled;" ui-sref="step3" type="button" class="btn btn-primary btn-circle active-step-wizard">3</a>
    </div>
    <div class="stepwizard-step">
        <a ng-disabled="step3Disabled;" ui-sref="step4" type="button" class="btn btn-primary btn-circle active-step-wizard">4</a>            
    </div>
    <div class="stepwizard-step">
        <a ng-disabled="step4Disabled;" ui-sref="step5" type="button" class="btn btn-primary btn-circle active-step-wizard">5</a>            
    </div>
    <div class="stepwizard-step">
        <a ng-disabled="step5Disabled;" ui-sref="step6" type="button" class="btn btn-primary btn-circle active-step-wizard">6</a>
    </div>
</div>

但是,只要当前步骤的表单验证有效,使用此方法就不会$scope.step2Disabled = true; $scope.step3Disabled = true; $scope.step4Disabled = true; $scope.step5Disabled = true; 下一步。我该如何解决这个问题?感谢

更新

我尝试这个解决方案:

测试1:

enabled

测试1结果:

它只适用于当前步骤中的Im,<div class="stepwizard-step"> <a ng-disabled="step3Disabled;step3Form.$invalid;" ui-sref="step4" type="button" class="btn btn-primary btn-circle active-step-wizard">4</a> </div> 在表单验证有效时正常工作

测试2:

ng-disabled

测试2结果:

当我在上一步时,完全禁用此步骤的链接。但是当我在此步骤并且表单有效时,仍然禁用到下一步的链接。

2 个答案:

答案 0 :(得分:0)

您无法对整个表单进行验证。您需要检查每个输入是否为$ invalid。也许你创建了一个函数或者某个东西来检查表单的每个输入。

编辑:我回答错误抱歉^^。因此,这将是您的问题的解决方案:https://jsbin.com/sojejunazo/edit

答案 1 :(得分:0)

你的控制器中应该有一个$ scope变量:

$scope.currentStep = 0;
$scope.enableCount = 0;

然后你有一个功能

$scope.changeStep = function(index){
    $scope.currentStep = index;
}
在提交函数中

添加以下代码行:

if(form.$valid && $scope.enableCount <= $scope.currentStep) $scope.enableCount++;

然后在你的HTML中你应该这样做:

<div class="stepwizard-row setup-panel">
    <div class="stepwizard-step">
        <a ui-sref="step1" type="button" ng-click="changeStep(0)" class="btn btn-primary btn-circle active-step-wizard">1</a>
    </div>
    <div class="stepwizard-step">
        <a ng-disabled="enableCount < 1" ng-click="changeStep(1)" ui-sref="step2" type="button" class="btn btn-primary btn-circle active-step-wizard">2</a>
    </div>
    <div class="stepwizard-step">
        <a ng-disabled="enableCount < 2" ng-click="changeStep(2)" ui-sref="step3" type="button" class="btn btn-primary btn-circle active-step-wizard">3</a>
    </div>
    <div class="stepwizard-step">
        <a ng-disabled="enableCount < 3" ng-click="changeStep(3)" ui-sref="step4" type="button" class="btn btn-primary btn-circle active-step-wizard">4</a>            
    </div>
    <div class="stepwizard-step">
        <a ng-disabled="enableCount < 4" ng-click="changeStep(4)" ui-sref="step5" type="button" class="btn btn-primary btn-circle active-step-wizard">5</a>            
    </div>
    <div class="stepwizard-step">
        <a ng-disabled="enableCount < 5" ng-click="changeStep(5)" ui-sref="step6" type="button" class="btn btn-primary btn-circle active-step-wizard">6</a>
    </div>
</div>