从角度控制器访问模板元素

时间:2015-03-04 08:27:26

标签: javascript html angularjs

我有一个像这样的HTML模板

<div class="row form-field" ng-show="specialRequestShown" id="specialRequests">
            <div>
                <label><input type="checkbox" class="needsclick"> Non-smoking Room</label>
            </div>
            <div>
                <label><input type="checkbox" class="needsclick"> Late Check-in</label>
            </div>
            <div>
                <label><input type="checkbox" class="needsclick"> Early Check-in</label>
            </div>
            <div>
                <label><input type="checkbox" class="needsclick"> Twin Bed</label>
            </div>
            <div>
                <label><input type="checkbox" class="needsclick"> Large Bed</label>
            </div>
            <div>
                <label><input type="checkbox" class="needsclick"> Extra Bed</label>
            </div>
            <div>
                <label><input type="checkbox" class="needsclick"> Airport Transfer</label>
            </div>
        </div>

        <button type="submit" class="btn btn-default gi-btn-next" ng-click="goToPayment()">
            Lanjutkan
        </button>
</div>

问题: 在函数goToPayment()中,我怎样才能在div specialRequests中选中哪个复选框而无需为每个复选框分配ng-model?函数goToPayment()位于控制器中。无论如何都要遍历div specialRequests孩子?

1 个答案:

答案 0 :(得分:3)

尝试以下任何一项:

$scope.goToPayment = function () {
    var checkedItems = document.querySelectorAll('#specialRequests input:checked')
}

$ element 注入您的控制器:

$scope.goToPayment = function () {
    var checkedItems = $element[0].querySelectorAll('input:checked')
}