【问题标题】:Remember selected checkboxes - AngularJS记住选中的复选框 - AngularJS
【发布时间】:2018-02-20 01:06:43
【问题描述】:

首先,所有复选框都被选中。 然后我只检查其中的 2 个并按搜索。

复选框和搜索按钮:

  <input type="checkbox" id="stat2" ng-true-value="true" ng-false-value="false" ng-model="ts.status.check2" />&nbsp;
  <label for="stat2">Status 2</label>
  <br />

  <input type="checkbox" id="stat3" ng-true-value="true" ng-false-value="false" ng-model="ts.status.check3" />&nbsp;
  <label for="stat3">Status 3</label>
  <br/>

  <input type="checkbox" id="stat1" ng-true-value="true" ng-false-value="false" ng-model="ts.status.check1" />&nbsp;
  <label for="stat1">Status 1</label>
  <br />

  <input type="checkbox" id="stat4" ng-true-value="true" ng-false-value="false" ng-model="ts.status.check4" />&nbsp;
  <label for="stat4">Status 4</label>
  <br />

然后一个新页面(新 URL)正在加载,在这个页面上有一个“返回”按钮。 当我按下后退按钮时,我回到了第一页,并且再次选中了所有 4 个复选框,我怎样才能使它成为可能,只有那些被选中,我首先检查了?

返回按钮

  <div class="btn-group container panel-footer">
    <button type="button" onClick="history.go(-1);return true;" class="btn">Back</button>
  </div>

【问题讨论】:

  • 在 $rootScope 中存储复选框
  • @ShubhamKandiyal 没有被弃用?对我来说,您应该更喜欢路由参数(UI 路由器或本机路由器)
  • $rootScope 或 localStorage 用于持久化。

标签: angularjs search checkbox


【解决方案1】:

使用 $rootScope 维护您的数据:

$rootScope.checkState = {
    check1: true,
    check2: false,
    check3: true,
    check4: false
};

然后在你看来:

<input type="checkbox" id="stat2" ng-true-value="true" ng-false-value="false" ng-model="$root.checkState.check1" />&nbsp;

(对所有带有相应字段的复选框执行此操作)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-02-04
    • 2011-07-28
    • 2016-07-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多