ng-if和ng-model不能一起使用

时间:2019-01-14 08:16:09

标签: angularjs

有一个具有两个复选框选项的表单,一个是英语,另一个是印地语。在语言选择的基础上,div(child form)将打开。但是我有一个条件,如果child表单或div具有某些值,则打开页面加载复选框将被选中,而div将处于打开状态。 我尝试的代码是-

filterChanged(entity: string, values: string[]) {
  this.activeFilters[entity] = values
  this.navigate()
}

navigate() {
  // gather parameters
  const queryParams = {}
  for (const filter of this.filters) {
    queryParams[filter.entity] = this.activeFilters[filter.entity]
  }
  this.router.navigate(
    [],
    {
      relativeTo: this.route,
      queryParams: queryParams,
      queryParamsHandling: 'merge'
  })
}

这是我的div

     <label class="radio-inline" ng-if="$ctrl.astrologerLanguageDetails.IsEnglishValue === true" >
                     English <input type="checkbox" ng-checked="true"><br /><br />
                 </label>

                 <label class="radio-inline" ng-if="!$ctrl.astrologerLanguageDetails.IsEnglishValue === true">
                     English <input type="checkbox" ng-model="ENchkselct" ><br /><br />
                 </label>

此代码仅在选中复选框时有效,但是当我选中复选框时它不会显示div。我是angularjs的新手,请指导我。 我会尝试自动打开div(如果复选框处于选中状态),否则,当用户选中该复选框时,它将显示div

1 个答案:

答案 0 :(得分:0)

AngularJs在ng-if内创建一个子$ scope,因此您应使用$parent对象访问父控制器$ scope。

代码示例:

    <label class="radio-inline" ng-if="$ctrl.astrologerLanguageDetails.IsEnglishValue === true">
        English <input type="checkbox" ng-checked="true"><br /><br />
   </label>

   <label class="radio-inline" ng-if="!$ctrl.astrologerLanguageDetails.IsEnglishValue === true">
        English <input type="checkbox" ng-model="$parent.ENchkselct" ><br /><br />
   </label>
相关问题