【问题标题】:angularJS ngDisabled executes function before ng-repeat loadsangularJS ngDisabled 在 ng-repeat 加载之前执行函数
【发布时间】:2015-12-19 00:00:14
【问题描述】:

我正在使用 API 通过$http 填充$scope.var,并采用$scope.var

我有一个嵌套的ng-repeat

html:

<tr ng-repeat='(chore,details) in choreList'>
  <td>
    <div class="btn-group">
      <button ng-repeat="user in users" class='btn group-item' ng-click="addChoreDetail(chore, user.name)" title="{{user.name}}" ng-disabled="checkAddabilityChoreDetail(chore, user.name)">{{chore}}</button>
          </div>
        </td>
      </tr>

js:

$scope.checkAddabilityChoreDetail = function(inputChore,inputName){
  console.log(inputChore);
}

我不知道在 Angular 中如何让 ngDisabled 等待发送杂项变量,直到重复实际发生。

我在控制台日志中收到 get choreList 的请求时间的 undefined,但我不确定如何让它等待我的对象填充数据。

【问题讨论】:

  • “家务”从何而来?
  • 你有 ng-repeat 从 $scope.choreList 和 $scope.users 获取你的数组。确保您通过 API 填充这些范围变量...

标签: javascript angularjs


【解决方案1】:

您应该将 promises 用于不知道何时完成的异步操作 - http://docs.angularjs.org/api/ng.$q。

Promise 是“用于与表示异步执行的操作的结果的对象交互的接口,并且可能会或可能不会在任何给定时间点完成”。

【讨论】:

  • 这是一个很好的例子:stackoverflow.com/questions/21097677/…
  • 我认为 Andrew 不需要 $q 服务,因为他说他使用 $http 服务,他已经使用了 .then 回调,所以问题不应该是请求本身。我的怀疑是角度摘要,因为当 ng-repeat 中没有加载任何家务时,似乎会触发 ng-disabled 函数。如果 checkAddabilityChoreDetail 中有一个简单的条件,这应该没有问题。在将杂务放在范围内后,应该再次调用该函数,已经完成了杂务。然而,更多的代码会很好......
  • 我最终创建了一个新范围并在我的依赖承诺中处理范围人口。然后使用该新范围进行 ng-repeat。问题已解决:)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多