类型错误:未定义

问题描述:

我需要无法读取属性“typeRevueESR”是,我要输入“日期DE信号DE L'offre”被标记为“必需的”,如果选择“E - 签名杜contrat”被选中。类型错误:未定义

我不知道问题是什么,在我看来,一切都好。但是,它让我看到以下错误:

TypeError: Cannot read property 'typeRevueESR' of undefined at Scope.SupportDemandeCtrl.$scope.matchSelectedTypeRevueESR (SupportDemandeApp.js:70)

这里是Plunker

SupportDemandeApp.js代码:

///<reference path="../Scripts/angular.min.js" /> 
///<reference path="../Scripts/angular-route.min.js" /> 

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

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

supportDemandeApp.run(function ($rootScope) { 
$rootScope.typeOf = function (value) { 
    return typeof value; 
}; 
}) 

supportDemandeApp.directive('stringToNumber', function() { 
return { 
    require: 'ngModel', 
    link: function (scope, element, attrs, ngModel) { 
     ngModel.$parsers.push(function (value) { 
      return '' + value; 
     }); 
     ngModel.$formatters.push(function (value) { 
      return parseFloat(value); 
     }); 
    } 
}; 
}); 

function SupportDemandeCtrl($scope, $filter) { 

//Fonction : Type de contrat (IS/OS) 
$scope.typeContrat = function() { 
    $scope.typesContrat = ['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.typeContrat === 'Niv.I - Impartition IMS') || 
     ($scope.selectedTypeContrat.typeContrat === 'Niv.II - Impartition TMA/AMS') || 
     ($scope.selectedTypeContrat.typeContrat === 'Niv.IV - Impartition BPO') || 
     ($scope.selectedTypeContrat.typeContrat === 'Multi-Tiers avec Impartition')) 
     return true; 
    else 
     return false; 
}; 

//Fonction : Date de signature de l’offre/Type Revue ESR 
$scope.typeRevueESR = function() { 
    $scope.typesRevueESR = ["A - Faire une offre ou non", 
          "B - Stratégie de l'offre", 
          "A/B", 
          "C - Soumission de l'offre", 
          "A/B/C", 
          "E - Signature du contrat" 
          ]; 

    $scope.selectedTypeRevueESR = {}; 
}; 

$scope.matchSelectedTypeRevueESR = function() { 
    if ($scope.selectedTypeRevueESR.typeRevueESR === 'E - Signature du contrat') 
     return true; 
    else 
     return false; 
}; 
} 

SupportDemande.cshtml:

<script src="~/ScriptsJS/SupportDemande.js" type="text/javascript"></script> 
<script src="~/ScriptsJS/SupportDemandeApp.js"></script> 

<div ng-app="supportDemandeApp" ng-controller="SupportDemandeCtrl"> 
    <form class="form-horizontal" id="supportDemandeForm" name="supportDemandeForm" method="post" ng-submit="validationSupportDemande(supportDemandeForm.$valid)" novalidate> 
     <fieldset> 
      <div class="form-group"> 
       <label for="selectTypeRevue" class="col-lg-2 control-label">Type de revue à réaliser<span style="color:red"> *</span></label> 
       <div class="col-lg-10"> 
        <select class="form-control" id="selectTypeRevue" name="selectTypeRevue" ng-model="selectedTypeRevue" required> 
         <option disabled hidden selected></option> 
         <option ng-option>ESR</option> 
         <option ng-option>SSR</option> 
        </select> 
      </div> 

      <div class="form-group" ng-if="selectedTypeRevue == 'ESR'"> 
       <label for="selectTypeRevueESR" class="col-lg-2 control-label">Type de revue ESR à réaliser<span style="color:red"> *</span></label> 
       <div class="col-lg-10"> 
        <select class="form-control" id="selectTypeRevueESR" name="selectTypeRevueESR" ng-model="selectedTypeRevueESR.typeRevueESR" ng-init="typeRevueESR()" ng-options="typeRevueESR for typeRevueESR in typesRevueESR" required></select> 
      </div> 

      <div class="form-group has-warning" ng-required="matchSelectedTypeRevueESR()"> 
       <label for="inputDateSignatureOffre" class="col-lg-2 control-label">Date de signature de l’offre</label> 
       <div class="col-lg-10"> 
        <input type="date" class="form-control" id="inputDateSignatureOffre" name="inputDateSignatureOffre" ng-model="inputDateSignatureOffre"> 
      </div> 
     </fieldset> 
    </form> 
</div> 

它告诉你究竟是什么问题。您尚未定义selectedTypeRevueESR,因此您无法对其设置属性typeRevueESR

我不知道您的具体应用程序的逻辑是什么在这里,但你可能需要先初始化这些对象是这样的:

$scope.selectedTypeRevueESR = { 
    typeRevueESR: "" 
}; 
+0

它不工作。我编辑了我的问题,并添加了我的需求和SupportDemande.cshtml – DjSadwell

+0

@DjSadwell,你在哪里定义了你的控制器? –

+0

$ scope.selectedTypeRevueESR = {}; – DjSadwell