angular.js - Mencari cara untuk menulis angularJS
仅有的幸福
仅有的幸福 2017-05-15 16:51:15
0
3
714


A ialah senarai juntai bawah dan BC ialah kotak teks.
Kotak teks C disembunyikan secara lalai. Kotak teks B tersedia.
Untuk mencapainya apabila A menarik ke bawah untuk memilih elemen terakhir, B dinyahdayakan dan C dipaparkan.
A tidak memilih elemen terakhir dan memulihkannya kepada keadaan asalnya.

            <fieldset>
            <legend>Household Income Survey</legend>

            <p class="row">
                <p class="form-group col-xs-12 col-sm-6">
                    <label for="HouseholdSize" class="control-label">I. How many people live in your home?</label>
                    <select class="form-control" name="HouseholdSize" id="HouseholdSize">
                        <option value="1">1</option>
                        <option value="2">2</option>
                        <option value="3">3</option>
                        <option value="4">4</option>
                        <option value="5">5</option>
                        <option value="6">6</option>
                        <option value="7">7</option>
                        <option value="8">8</option>
                        <option value="8+">8+</option>
                    </select>
                </p>
                <p class="form-group col-xs-12 col-sm-6">
                    <label for="HouseholdIncome" class="control-label">II. Household Annual Income</label>
                    <select class="form-control" name="HouseholdIncome" id="HouseholdIncome">
                        <option value=""></option>
                        <option value="15171">15171</option>
                        <option value="21590">21590</option>
                        <option value="29101">29101</option>
                        <option value="36612">36612</option>
                        <option value="44123">44123</option>
                        <option value="51634">51634</option>
                        <option value="59145">59145</option>
                        <option value="66656">66656</option>
                        <option value="74167">74167</option>
                    </select>
                </p>
            </p>

            <p class="form-group hidden" id="HouseholdIncomeGroup">
                <label class="control-label" for="HouseholdIncome">III. If there are more than 8 people living at your
                    home, please write the annual household income here</label>

                <p class="input-group">
                    <p class="input-group-addon">$</p>
                    <input type="text" class="form-control" name="" placeholder="">
                </p>
            </p>
        </fieldset>
仅有的幸福
仅有的幸福

membalas semua(3)
为情所困

kod html:

<fieldset ng-controller="IncomeCtrl">
    <legend>Household Income Survey</legend>

    <p class="row">
        <p class="form-group col-xs-12 col-sm-6">
            <label class="control-label">
                I. How many people live in your home?
            </label>
            <select class="form-control" ng-model="householdSize" ng-change="change()">
                <option value="1">1</option>
                <option value="2">2</option>
                <option value="3">3</option>
                <option value="4">4</option>
                <option value="5">5</option>
                <option value="6">6</option>
                <option value="7">7</option>
                <option value="8">8</option>
                <option value="8+">8+</option>
            </select>
        </p>
        <p class="form-group col-xs-12 col-sm-6">
            <label class="control-label">
                II. Household Annual Income
            </label>
            <select class="form-control" ng-model="householdIncome" ng-disabled="showDefIncom">
                <option value=""></option>
                <option value="15171">15171</option>
                <option value="21590">21590</option>
                <option value="29101">29101</option>
                <option value="36612">36612</option>
                <option value="44123">44123</option>
                <option value="51634">51634</option>
                <option value="59145">59145</option>
                <option value="66656">66656</option>
                <option value="74167">74167</option>
            </select>
        </p>
    </p>

    <p class="form-group" id="HouseholdIncomeGroup" ng-show="showDefIncom">
        <label class="control-label">
            III. If there are more than 8 people living at your
            home, please write the annual household income here
        </label>

        <p class="input-group">
            <p class="input-group-addon">$</p>
            <input type="text" class="form-control" ng-model="defHouseholdIncome">
        </p>
    </p>
</fieldset>

Kod JavaScript:

.controller('IncomeCtrl', function ($scope) {
    $scope.householdSize = 1;
    $scope.householdIncome = 15171;
    $scope.defHouseholdIncome = 0;
    $scope.showDefIncom = false;

    $scope.change = function () {
        $scope.showDefIncom = $scope.householdSize === '8+' ? true : false;
    };

})
阿神

Idea yang sama seperti @nightire, berikut ialah bahagian kod:

html<body ng-app="app">
    <p ng-controller="main">
        <select ng-options="item for item in items" ng-model="selVal" ng-init="selVal = items[0]"></select>
        <input type="text" ng-model="b" ng-disabled="selVal===items[items.length-1]" placeholder="b">
        <input type="text" ng-model="c" ng-show="selVal===items[items.length-1]" placeholder="c">
    </p>
    <script type="text/javascript">
    var app = angular.module('app', []);

    app.controller('main', ['$scope', function($scope) {

        $scope.items = ['1', '2', '3', '4', '5'];


    }]);
    </script>
</body>

Mungkin begini.

阿神

<select> diisi, kemudian ng-model mengikat atribut dalam skop, kemudian $watch atribut ini, menukar atribut keadaan (pratakrif) apabila syarat dipenuhi dan akhirnya mengikat atribut keadaan ini dalam kedua-dua arah Hanya berikan B ng-disabled dan C ng-show/ng-if.

Muat turun terkini
Lagi>
kesan web
Kod sumber laman web
Bahan laman web
Templat hujung hadapan