当前位置: 代码迷 >> JavaScript >> 订阅事件不会每次都被调用
  详细解决方案

订阅事件不会每次都被调用

热度:61   发布时间:2023-06-07 16:43:36.0

我有搜索屏幕,当用户导航到结果页面时,结果页面上有带有值(分钟,字符)的单选按钮。

我已经在结果页面上订阅了单选按钮单击事件,每当用户单击单选按钮订阅功能时,都应该调用该事件。

现在的问题是:

1)当用户第一次点击搜索按钮时,显示结果。

2)用户选择“分钟”单选按钮,订阅事件被调用并开始工作。

3)用户选择返回按钮,并且用户被重定向到搜索屏幕。

4)用户点击搜索按钮,结果显示。

5)用户试图单击“字符”按钮,没有调用订阅事件。

她的是我的代码:

<div class="nmc-righttab" style="width: 60%">
    <div class="nmc-righttab" style="margin-left:20px">
        <label class="nmc-label" style="line-height:21px">@Res.Trends.DisplayResultBy</label>
    </div>
    <div class="nmc-righttab" style="width:250px">
        <div class="nmc-lefttab" style="margin-left:5px">
            <input type="radio" id="MinuRadio" name="GphInterval" value="2" data-bind="checked:GraphClick" />
        </div>
        <div class="nmc-righttab">
            <label class="nmc-label" for="MinuRadio" style="line-height:21px">@Res.Trends.Minutes</label>
        </div>
        <div class="nmc-lefttab" style="margin-left:5px">
            <input type="radio" id="CharRadio" name="GphInterval" value="3" data-bind="checked:GraphClick" />
        </div>
        <div class="nmc-righttab">
            <label class="nmc-label" for="CharRadio" style="line-height:21px">@Res.Trends.Characters</label>
        </div>
    </div></div>

JS代码:

  this.GraphClick.subscribe(function (val) {
      model.showGraphResults(true);
      model.GraphIntervalSelected(val);
      searchSpeechGraphView(speechUsage);
  }, this)

与其将this参数作为参数传递给subscribe方法,不如将其与this对象绑定在一起。 像这样

JS代码:

this.GraphClick.subscribe(function (val) {
  model.showGraphResults(true);
  model.GraphIntervalSelected(val);
  searchSpeechGraphView(speechUsage);
}.bind(this));
  相关解决方案