2016-05-20 13 views
0

私があなたを助けてくれることを願っています。ng-modelフィールドの値を条件付き変更に基づいて変更してください

(一見)単純な条件文に基づいて私のAngularJSビューフィールドの値を設定したいと思います。

私は答えがとてもシンプルになると信じていますが、1時間以上の検索と試してもそれを見つけることはできませんでした。

私は間違った指示(私はng-ifとng-changeのようなものを試しました)を使用しようとしているかもしれないと思います。

私は(ng-if="reportSettingsData.ReportFrequencyName.ReportFrequencyId == Enum.ReportFrequency.Weekly)週刊するレポートの頻度を変更は私のNG-モデルフィールドreportSettingsData.ReportFrequencyWeekdayName = "None"の値を設定します。

は、これは私が達成したいものです。

およびその逆。レポートの頻度をMonthlyに変更すると、reportSettingsData.ReportFrequencyMonthDayName = "None"の値が設定されます。(ng-if="reportSettingsData.ReportFrequencyName.ReportFrequencyId != Enum.ReportFrequency.Weekly)

<!-- Frequency --> 
<div class="col-md-3"> 
    <label>Frequency</label> 
    <div class="input-dropdown"> 
     <cc-dropdown cc-placeholder="Report Frequency" 
         ng-model="reportSettingsData.ReportFrequencyName" 
         ng-options="reportSettingsData.SelectableReportFrequencyNames" 
         cc-fields="ReportFrequencyName" 
         ng-change="frequencyChanged()" 
         cc-key-field="ReportFrequencyId" 
         cc-allow-search="reportSettingsData.SelectableReportFrequencyNames != null && reportSettingsData.SelectableReportFrequencyNames.length > 5" 
         name="iFrequencyName"> 
     </cc-dropdown> 
    </div> 
</div> 

<!-- Week Days/Month Days --> 
<div class="col-md-3"> 
    <label>Frequency Options</label> 
    <div class="input-dropdown" ng-if="reportSettingsData.ReportFrequencyName.ReportFrequencyId != Enum.ReportFrequency.Weekly"> 
     <cc-dropdown cc-placeholder="Report Frequency Option" 
         ng-model="reportSettingsData.ReportFrequencyMonthDayName" 
         ng-options="reportSettingsData.SelectableReportFrequencyMonthDays" 
         ng-disabled="reportSettingsData.ReportFrequencyName.ReportFrequencyId != Enum.ReportFrequency.Monthly" 
         cc-fields="ReportFrequencyMonthDayName" 
         cc-key-field="ReportFrequencyMonthDayId" 
         cc-allow-search="true" 
         name="iFrequencyMonthDays"> 
     </cc-dropdown> 
    </div> 

    <div class="input-dropdown" ng-if="reportSettingsData.ReportFrequencyName.ReportFrequencyId == Enum.ReportFrequency.Weekly"> 
     <cc-dropdown cc-placeholder="Report Frequency Option" 
         ng-model="reportSettingsData.ReportFrequencyWeekdayName" 
         ng-options="reportSettingsData.SelectableReportFrequencyWeekdays" 
         cc-fields="ReportFrequencyWeekdayName" 
         cc-key-field="ReportFrequencyWeekdayId" 
         cc-allow-search="true" 
         name="iFrequencyWeekdays"> 
     </cc-dropdown> 
    </div> 
</div> 

大変ありがとうございます!

答えて

1

条件に基づいてdom要素を削除または作成するためにNg-if命令を使用する場合は、https://docs.angularjs.org/api/ng/directive/ngIfを参照してください。

ご使用の場合は必須ではありません。

変更イベントにバインドし、関数内でモデルフィールドに適切な値を設定します。

$scope.frequencyChanged = function(){ 
    if($scope.reportSettingsData.ReportFrequencyName.ReportFrequencyId == Enum.ReportFrequency.Weekly){ 
    $scope.reportSettingsData.ReportFrequencyWeekdayName = "None". 
    } 
}; 

これが役立ちます。

+0

私はあなたの提案を実装しており、意図した目的のために働いていますが、期待通りのものではありません。あなたが喜んでいるなら、私の編集された質問を見てください。ありがとう! – onmyway

関連する問題