为什么 ng-change 总是从模型传递 'true'
Why does ng-change always pass 'true' from model
当使用 ng-change 指令并将模型信息传递给函数时,ng-change 始终传递:true;不管开关是否为 on/off。我想根据 on/off 状态接收 true 和 false 两种状态。
我正在使用基于 switchery and angular-ui-switch
的自定义开关指令
<switch
ng-model="app.layout.isNavbarFixed"
ng-change="uiSettingTypeChange('isNavbarFixed',
{{app.layout.isNavbarFixed}})"
class="green">
</switch>
和
$scope.uiSettingTypeChange = function(name, setting) {
console.log(name +' | ' + setting);
};
console.log 总是输出:
isNavbarFixed | true
ng-change 属性的值已经是一个 angular 表达式,因此不需要 {{}}
。我猜正在发生的事情是 angular 看到了 {{}}
并因此对其进行了评估。所以,此时 ng-change 有效看起来像这样:
ng-change="uiSettingTypeChange('isNavbarFixed',true)"
然后,当 change 事件触发时,它只是每次都传递 true。你想要的应该看起来更像这样:
ng-change="uiSettingTypeChange('isNavbarFixed',app.layout.isNavbarFixed)"
当使用 ng-change 指令并将模型信息传递给函数时,ng-change 始终传递:true;不管开关是否为 on/off。我想根据 on/off 状态接收 true 和 false 两种状态。
我正在使用基于 switchery and angular-ui-switch
的自定义开关指令<switch
ng-model="app.layout.isNavbarFixed"
ng-change="uiSettingTypeChange('isNavbarFixed',
{{app.layout.isNavbarFixed}})"
class="green">
</switch>
和
$scope.uiSettingTypeChange = function(name, setting) {
console.log(name +' | ' + setting);
};
console.log 总是输出:
isNavbarFixed | true
ng-change 属性的值已经是一个 angular 表达式,因此不需要 {{}}
。我猜正在发生的事情是 angular 看到了 {{}}
并因此对其进行了评估。所以,此时 ng-change 有效看起来像这样:
ng-change="uiSettingTypeChange('isNavbarFixed',true)"
然后,当 change 事件触发时,它只是每次都传递 true。你想要的应该看起来更像这样:
ng-change="uiSettingTypeChange('isNavbarFixed',app.layout.isNavbarFixed)"