离子选择不会更新依赖于范围变量的函数 [英] ionic select does not update function dependent on scope variable

查看:93
本文介绍了离子选择不会更新依赖于范围变量的函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个离子选择框,用于更新范围变量。我还有一个范围函数,它是一个依赖于范围变量的函数(例如,它测试变量是否是特定值)。该函数的结果似乎没有使用离子选择框更新,而它似乎确实在基本angularJs中更新。嵌入实际条件而不是函数似乎适用于离子。

I have a ionic select box which updates a scope variable. I also have a scope function which is a function which is dependent on that scope variable (eg. it tests if it the variable is a specific value). The result of the function does not seem to update with the ionic select box, while it does seem to update in basic angularJs. Embedding the actual condition instead of the function seems to work for ionic.

离子示例: http://codepen.io/anon/pen/VeOXzb

控制器中的相关javascript:

Relevant javascript in controller:

$scope.testValue = 'value1';

$scope.variableFunction1 = function() {
  return $scope.testValue === 'value2';
}

相关html:

Does not change: {{variableFunction1()}}<br/>
Does change: {{testValue === 'value2'}}<br/>

<div class="item item-input item-select">
  <div class="input-label">
    testValue
  </div>
  <select ng-model="testValue">
    <option value="value1">Val1</option>
    <option value="value2">Val2</option>
    <option value="value3">Val3</option>
  </select>
</div>

同样的角度示例,它按预期工作: http://jsfiddle.net/mm5vg0oa/

The same angular example, where it works as I expect: http://jsfiddle.net/mm5vg0oa/

这是一个错误还是我误解了离子中的东西?

Is this a bug or am I misunderstanding something in ionic?

推荐答案

我的猜测是 ion-content 创建一个新的子范围,这意味着当您在select中选择一个值时,在 ion-content 范围内设置一个新的testValue变量(与控制器范围不同) )。

My guess is that ion-content creates a new child scope, which means that when you select a value in the select, a new testValue variable is set on the ion-content scope (which is different from the controller scope).

您有两种选择:


  1. 设置 ng-controller ion-content 内的元素上。

  2. 使用一个对象来保存选定的值,而不是字符串(请参阅此处的示例: http://codepen.io/anon/pen/ WrBzBq )。

  1. Set the ng-controller on an element inside the ion-content.
  2. Use an object to hold the selected value, instead of a string (See example here: http://codepen.io/anon/pen/WrBzBq).

此页面很好地解释了问题: https://github.com/angular/angular.js/wiki/Understanding-Scopes

This page explains the problem pretty well: https://github.com/angular/angular.js/wiki/Understanding-Scopes

这篇关于离子选择不会更新依赖于范围变量的函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆