如何从View中有条件地为ViewModel属性赋值

时间:2015-04-10 01:23:23

标签: angularjs

我有以下HTML,在满足ng-show条件时显示错误消息。但是,无论是否显示错误消息,都会发生ng-init分配。

<div ng-show="peopleworksForm.submitted && formData['DisclosureQuestion'+($index+1)+'Answer'] == 'true' && formData['DisclosureQuestion'+($index+1)+'OptionalAnswer'] == undefined" 
     ng-init="$parent.answerValFailed = true">
   invalid value
</div>

我想要的是仅在满足ng-init条件后才在ng-show内进行操作。有没有简单的方法呢?

1 个答案:

答案 0 :(得分:0)

使用ng-if及其内部使用ng-init:)

    <div class="pw-form-inline-error" ng-if="peopleworksForm.submited && formData['DisclosureQuestion'+($index+1)+'Answer'] == 'true' && formData['DisclosureQuestion'+($index+1)+'OptionalAnswer'] == undefined">
            <span ng-init="$parent.answerValFailed = true">
                            invalid value
              </span>
    </div>

希望有所帮助:)