Angular 1 / ng-如果仅在条件下进行一次性绑定

时间:2017-07-25 14:57:01

标签: javascript angularjs ui-select angular-ui-select angularjs-ng-if

在我的模板中,我有:

...
info: Server.Controllers.HomeController[0]
      Cookie coming
info: Server.Controllers.HomeController[0]
      347
info: Server.Controllers.HomeController[0]
  CfDJ8GSLZENXaNpNrtmz2DAt9joqJ6CEHpCFbJdbNxbQYjjoQmd4naOI0L0krNMSQdVhqPRP9tJJMMIRayc5ILRQMcJQWNZ0T9Fjuk7Qxg65wPP7SR43UZxwy6vGQ7_qeSp44gYLLe4NGEalhXynZxmD-jywqL4VJZ5y4OwpsEKLx-VVT03xAlt54J_qQk_O4wjwLQiZBpAVTFKUWN4u7H8yd_rwMTIGBPu21t5n35To9bTQU5677xNxiEFap3ukuxO4p-OxVakXqShy2Xk_vYDAvv_XFV6jgNcy4ZiCRB8VUhXGcNr205h4X0-O7JHB8mYbc13aZLmrAwvG5DWTBd3_OCo
...
info: Server.Controllers.HomeController[0]
      Cookie coming
info: Server.Controllers.HomeController[0]
      THE COOKIE IS NULL

在我的组件中

<div ng-if="$ctrl.show()">
  <input class="form-control" type="text">
</div>

我希望angular只在单击属性 show() { if (angular.isDefined(this.parking.parkingType)) { return this.parking.parkingType.labelKey === 'parking_type.air' } } 的选择输入(ui-select)时处理该函数:

on-select="$ctrl.show()"

此案例可能类似于以下示例:仅在单击 <ui-select ng-model="$ctrl.parking.parkingType" on-select="$ctrl.show()"> <ui-select-match allow-clear="true"> <span>{{ $select.selected.label }}</span> </ui-select-match> <ui-select-choices repeat="item in $ctrl.parkingType | filter: { label: $select.search }"> <span ng-bind-html="item.label"></span> </ui-select-choices> </ui-select> 时启动该功能

1 个答案:

答案 0 :(得分:1)

将您的ng-show更改为变量并按原样保留on-select="$ctrl.show()"

在您看来:

<div ng-if="$ctrl.shouldShow">
  <input class="form-control" type="text">
</div>

在您的组件中:

$ctrl.show = function() {
  if (angular.isDefined(this.parking.parkingType)) {
    $ctrl.shouldShow = (this.parking.parkingType.labelKey === 'parking_type.air')
  }
}

不要在ng-if,ng-show和ng-hide中使用功能,这是一个很好的做法,因为它会影响性能