在HTML5 / AngularJS输入中显示除法计算结果

时间:2017-04-26 08:26:41

标签: angularjs html5 twitter-bootstrap-3 angularjs-1.6

我在ng-if和ng-options之间存在冲突。

函数montantAnnuel()在没有ng-if的情况下运行良好,但是当我使用ng-if显示一组2个输入时,它就不再起作用了。

App.js:

var SupportDemandeApp = angular.module('SupportDemandeApp', ['ngMessages']);

SupportDemandeApp.controller('SupportDemandeCtrl', ['$scope', SupportDemandeCtrl]);

function SupportDemandeCtrl($scope) {

$scope.typeContrat = function () {
    $scope.types = ['Niv.I - Impartition IMS',
                    'Niv.II - Impartition TMA / AMS',
                    'Niv.III - Projet',
                    'Niv.IV - Impartition BPO',
                    'Multi - Tiers avec Impartition',
                    'Multi-Tiers CS & Projet',
                    'Vente de licence ou produit sans services ni modification'];
    $scope.selectedTypeContrat = {};
};

$scope.matchSelectedTypeContrat = function () {
    if (($scope.selectedTypeContrat.type == 'Niv.I - Impartition IMS') ||
        ($scope.selectedTypeContrat.type == 'Niv.II - Impartition TMA / AMS') ||
        ($scope.selectedTypeContrat.type == 'Niv.IV - Impartition BPO') ||
        ($scope.selectedTypeContrat.type == 'Multi-Tiers avec Impartition'))
        return true;
    else
        return false;
};

$scope.inputMontantAnnuel = 0;

$scope.montantAnnuel = function () {
    if (angular.isUndefined($scope.inputMontantGlobalAffaire) && angular.isUndefined($scope.inputDureeAnnee)) {
        $scope.inputMontantAnnuel = 0;
    }
    else if (angular.isUndefined($scope.inputDureeAnnee)) {
        $scope.inputMontantAnnuel = $scope.inputMontantGlobalAffaire;
    }
    else if ($scope.inputMontantGlobalAffaire && $scope.inputDureeAnnee) {
        $scope.inputMontantAnnuel = ($scope.inputMontantGlobalAffaire / $scope.inputDureeAnnee);
    }
};
}

Index.cshtml:

<div ng-app="App" ng-controller="Ctrl">
<form class="form-horizontal" id="supportDemandeForm" name="supportDemandeForm" method="post" ng-submit="validationSupportDemande(supportDemandeForm.$valid)" novalidate>
    <fieldset>
<div class="form-group" ng-class="{ 'has-error': supportDemandeForm.inputMontantGlobalAffaire.$touched && supportDemandeForm.inputMontantGlobalAffaire.$invalid }">
                <label for="inputMontantGlobalAffaire" class="col-lg-2 control-label">Montant global prévu de l'affaire (en K€)<span style="color:red"> *</span></label>
                <div class="col-lg-10">
                    <input type="number" class="form-control" id="inputMontantGlobalAffaire" name="inputMontantGlobalAffaire" ng-model="inputMontantGlobalAffaire" ng-change="montantAnnuel()" pattern="[0-9]+(\\.[0-9][0-9]?)?" data-toggle="popover" data-trigger="hover" data-placement="right" data-content="Build + RUN + Infrastructure" required>
                    <div class="help-block" ng-messages="supportDemandeForm.inputMontantGlobalAffaire.$error" ng-if="supportDemandeForm.inputMontantGlobalAffaire.$touched"><div ng-message="required">Champ requis</div></div>
                </div>
            </div>

            <div class="form-group" ng-class="{ 'has-error': supportDemandeForm.selectTypeContrat.$touched && supportDemandeForm.selectTypeContrat.$invalid }">
                <label for="selectTypeContrat" class="col-lg-2 control-label">Type de contrat (IS/OS)<span style="color:red"> *</span></label>
                <div class="col-lg-10">
                    <select class="form-control" id="selectTypeContrat" name="selectTypeContrat" ng-model="selectedTypeContrat.type" ng-init="typeContrat()" ng-change="montantAnnuel()" ng-options="type for type in types" data-toggle="popover" data-trigger="hover" data-placement="right" data-content="Voir définitions dans l'onglet Type de contrat" required></select>
                    <div class="help-block" ng-messages="supportDemandeForm.selectTypeContrat.$error" ng-if="supportDemandeForm.selectTypeContrat.$touched"><div ng-message="required">Champ requis</div></div>
                </div>
            </div>

            <div class="form-group check-element" ng-if="matchSelectedTypeContrat()==true" ng-class="{ 'has-error': supportDemandeForm.inputDureeAnnee.$touched && supportDemandeForm.inputDureeAnnee.$invalid }">
                <div>
                    <label for="inputDureeAnnee" class="col-lg-2 control-label">Durée (en années)<span style="color:red"> *</span></label>
                    <div class="col-lg-10">
                        <input type="number" class="form-control" id="inputDureeAnnee" name="inputDureeAnnee" placeholder="" ng-model="inputDureeAnnee" ng-change="montantAnnuel()" required>
                        <div class="help-block" ng-messages="supportDemandeForm.inputDureeAnnee.$error" ng-if="supportDemandeForm.inputDureeAnnee.$touched"><div ng-message="required">Champ requis</div></div>
                    </div>
                </div>

                <div>
                    <label for="inputMontantAnnuel" class="col-lg-2 control-label">Montant annuel (en K€)</label>
                    <div class="col-lg-10">
                        <input readonly type="number" class="form-control" id="inputMontantAnnuel" name="inputMontantAnnuel" ng-model="inputMontantAnnuel" />
                    </div>
                </div>
            </div>
    </fieldset>
</form>

2 个答案:

答案 0 :(得分:0)

您可能需要初始化变量并在select:

上调用ng-change
$scope.inputDureeAnnee = 0;
$scope.inputMontantGlobalAffaire = 0;

<select class="form-control" id="selectTypeContrat" name="selectTypeContrat" ng-model="selectedTypeContrat.type" ng-init="typeContrat()" ng-options="type for type in types" required ng-change="montantAnnuel()"> </select>

因为当您显示div或更新字段时,您可能会尝试对undefined值执行操作。

其他事项:

  • 而不是isNaN,请使用angular.isUndefined
  • 而不是ng-if="selectedTypeContrat.type == 'Niv.I - Impartition IMS' || selectedTypeContrat.type == 'Niv.II - Impartition TMA / AMS' || selectedTypeContrat.type == 'Niv.IV - Impartition BPO' || selectedTypeContrat.type == 'Multi-Tiers avec Impartition'",使用函数ng-if="matchSelectedType()",此函数继续执行测试并返回truefalse

或者你也可以这样做,但你的模型不能反映初始值(你必须像这样访问它:selectedTypeContrat.type.name):

$scope.types = [
        {name: 'Niv.I - Impartition IMS', displayDiv: true},
        {name: 'Niv.II - Impartition TMA / AMS', displayDiv: true},
        {name: 'Niv.III - Projet', displayDiv: false},
        {name: 'Niv.IV - Impartition BPO', displayDiv: true},
        {name: 'Multi - Tiers avec Impartition', displayDiv: true},
        {name: 'Multi-Tiers CS & Projet', displayDiv: false},
        {name: 'Vente de licence ou produit sans services ni modification', displayDiv: false}
    ];


<select class="form-control" id="selectTypeContrat" name="selectTypeContrat" ng-model="selectedTypeContrat.type" ng-init="typeContrat()" ng-options="type as type.name for type in types" required></select>

ng-if="selectedTypeContrat.type && selectedTypeContrat.type.displayDiv  === true"

最后让这个工作,看到这个fiddle 不得不使用ng-show代替ng-if

答案 1 :(得分:0)

montantAnnuel()的小改进:

$scope.montantAnnuel = function () {
    if (angular.isUndefined($scope.inputMontantGlobalAffaire))
        $scope.inputMontantAnnuel = 0;
    else if (angular.isUndefined($scope.inputDureeAnnee)) {
        $scope.inputMontantAnnuel = $scope.inputMontantGlobalAffaire;
    } else {
        $scope.inputMontantAnnuel = ($scope.inputMontantGlobalAffaire / $scope.inputDureeAnnee);
    }
};