简体   繁体   English

在AngularJS中有条件地绑定数据

[英]Conditionally bind data in AngularJS

I have an array of tasks. 我有很多任务。 They have titles and and labels. 它们具有标题和标签。

function Task(taskTitle, taskType) {
  this.title = taskTitle;
  this.type = taskType;
}

$scope.tasks = [];

I end up declaring a bunch of tasks with different types and adding them to the array 我最终声明了一堆具有不同类型的任务,并将它们添加到数组中

In my html, I show a column of cards, filtered by type of task: 在我的html中,我显示了一列卡片,按任务类型过滤:

<div ng-model="tasks">
  <div class="card" ng-repeat="abc in tasks track by $index" ng-show="abc.type==0">
    <p> {{ abc.title }} </p>
  </div>
</div>

I want to bind the first card displayed in this filtered view to some other div . 我想将此过滤视图中显示的第一张卡片绑定到其他div I'll be processing an inbox, so I'll whittle this list of cards down to zero. 我将处理一个收件箱,因此将这张卡清单减少到零。 Each time I 'process' a card and remove it from the list, I need the data to refresh. 每次我“处理”一张卡并将其从列表中删除时,都需要刷新数据。

<div ng-model="firstCardInFilteredArray">
  <h4>Title of first card:</h4>
  <p> This should be the title of the first card! </p>
</div>

My intuition was to do something like this (in javascript): 我的直觉是这样做(在javascript中):

// pseudo-code!
$scope.inboxTasks = [];
for (i=0; i<tasks.length(); i++) {
  if (tasks[i].type == 0) {
    inboxTasks.append(tasks[i]);
  }
}

and somehow run that function again any time the page changes. 并在页面更改时以某种方式再次运行该功能。 But that seems ridiculous, and not within the spirit of Angular. 但这似乎很荒谬,而且不在Angular的精神范围内。

Is there a simple way in pure javascript or with Angular that I can accomplish this conditional binding? 在纯JavaScript或Angular中,有没有一种简单的方法可以完成此条件绑定?

You can filter your ng-repeat: https://docs.angularjs.org/api/ng/filter/filter 您可以过滤ng-repeat: https : //docs.angularjs.org/api/ng/filter/filter

<div ng-model="tasks">
  <div class="card" ng-repeat="abc in filteredData = (tasks | filter: {type==0}) track by $index">
    <p> {{ abc.title }} </p>
  </div>
</div>

Additionally, by saving the filtered data in a separate list you can display the next task like this: 此外,通过将过滤后的数据保存在单独的列表中,您可以显示下一个任务,如下所示:

<div>
  <h4>Title of first card:</h4>
  <p> filteredData[0].title </p>
</div>

Your data will automatically update as you "process" tasks. 您的数据将在您“处理”任务时自动更新。

To update inboxTasks, you could use $watchCollection : 要更新inboxTasks,可以使用$watchCollection

$scope.inboxTasks = [];

$scope.$watchCollection('tasks', function(newTasks, oldTasks)
{
   for (i=0; i<newTasks.length(); i++) 
   {
      if(newTasks[i].type == 0) 
      {
         $scope.inboxTasks.append(tasks[i]);
      }
   }
});

The other answers helped point me in the right direction, but here's how I got it to work: 其他答案帮助我指出了正确的方向,但这是我如何使其工作的方法:

HTML HTML

<input ng-model="inboxEditTitle" />

JS JS

$scope.filteredArray = [];
$scope.$watch('tasks',function(){
       $scope.filteredArray = filterFilter($scope.tasks, {type:0});
       $scope.inboxEditTitle = $scope.filteredArray[0].title;
    },true); // the 'true' keyword is the kicker

Setting the third argument of $watch to true means that any changes to any data in my tasks array triggers the watch function. $watch的第三个参数设置为true意味着对我的tasks数组中的任何数据进行的任何更改都会触发watch函数。 This is what's known as an equality watch , which is apparently more computationally intensive, but is what I need. 这就是所谓的“ 相等监视” ,它显然需要更多的计算量,但这正是我所需要的。

This SO question and answer has helpful commentary on a similar problem, and a great fiddle as well. 这样的问题和解答对类似的问题提供了有用的评论,也有很多技巧。

More on different $watch functionality in Angular 有关Angular中不同$ watch功能的更多信息

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM