简体   繁体   English

跨ng-repeat共享的$ scope变量

[英]$scope variable shared across ng-repeat

I have the following html 我有以下HTML

<section class="result" ng-repeat="record in results | orderBy: sortRecordByTimestamp" ng-init="highlights(record)" ng-class="level">
          <!-- some stuff -->
</section>

My scope has $scope.level = '' on init. 我的范围在init上有$scope.level = '' Functions are: 功能是:

function highlights(record) {

  // will contain more logic
  console.log('Highlighting...');
  level(record);

}

and

function level(record) {

  $scope.spreadsheet.forEach(function(row) {
    if (row.Name.indexOf(record.event) > -1) {
      $scope.level = 'level' + row.Level;
    }
  });

}

It correctly sets the $scope.level like it is supposed to, however, it is shared across all my results. $scope.level$scope.level那样正确设置$scope.level ,但是,它在我的所有结果中共享。

Isn't ng-repeat creating child scopes, with primitives being overwritten? ng-repeat是否不创建带有原始图元的子范围?

EDIT: 编辑:

Changed the html: 更改了html:

<section class="result" ng-repeat="record in results | orderBy: sortRecordByTimestamp" ng-class="level(record)">
  <!-- code -->
</section>

and the js: 和js:

$scope.level = function(record) {

  $scope.spreadsheet.forEach(function(row) {
    if (row.Name.indexOf(record.event) > -1) {
      console.log('Highlighting: level' + row.Level);
      return 'level' + row.Level;
    }
  });

}

It is logging correct values, however, ng-class is never set. 它正在记录正确的值,但是从未设置ng-class。 Why? 为什么?

EDIT 2: 编辑2:

To clarify what I am trying to do: 为了阐明我要做什么:

$scope.spreadsheet contains data to compare to record data. $scope.spreadsheet包含要比较以record数据的数据。 Match can be found in 3 instances, each enumerated in row.Level depending on row.Name . 可以在3个实例中找到匹配项,每个实例都在row.Level枚举,具体取决于row.Name So level(record) returns a css class of level1 , level2 or level3 . 因此, level(record)返回一个css类level1level2level3

These have a background: /* color */ property set to them. 这些具有background: /* color */设置为它们的属性。 So each row that has record.event found in the row.Name (they don't match exactly, that's why no === ) is colored based on which level of importance it is given. 因此,已各行record.event在发现row.Name (他们不完全匹配,这就是为什么没有=== )的基础上,其重要程度它被赋予着色。

The first time I tried to make the child scopes inherit the $scope.level property and determine it on element creation (that's why ng-init ), then I made $scope.level a function that returns the level. 第一次尝试使子作用域继承$scope.level属性并在创建元素时确定它(这就是ng-init的原因),然后我使$scope.level一个返回该级别的函数。 However, it fires on ALL records with the result of the last iteration. 但是,它会以最后一次迭代的结果在所有记录上触发。

How do I make determining the level separate for each record? 如何确定每个记录的级别?

ng-repeat doesn't create an isolated scope, it does create an child scope which is prototypically inherited from its parent scope. ng-repeat不会创建隔离的作用域,而是会创建一个子作用域,该作用域原型上是从其父作用域继承的。 If you look at ng-repeat API you will found that ng-repeat directive has scope: true in it, which is responsible for creating prototypically inherited scope. 如果查看ng-repeat API,您会发现ng-repeat指令中具有scope: true ,它负责创建原型继承的范围。

To access the property of parent scope inside child scope, you should have declared object type, so that as per dot rule you could access its child properties inside child. 要访问子作用域内的父作用域的属性,您应该已声明object类型,以便按照点规则可以访问子作用域内的子作用域。 If you are using primitive types then you should have mention $parent. 如果使用primitive类型,则应提及$parent. to get access to parent scope variable. 获取对父作用域变量的访问。

There are various way provided by ng-repeat API to keep track on which which element you are, $index in place on which index element you are in. ng-repeat API提供了多种方式来跟踪您位于哪个元素上, $index处于您位于哪个index元素上。

$index number iterator offset of the repeated element (0..length-1) $index number重复元素的迭代器偏移量(0..length-1)

$first boolean true if the repeated element is first in the iterator. $first boolean如果重复元素在迭代器中位于第一个, $first true。

$middle boolean true if the repeated element is between the first and last in the iterator. $middle boolean如果重复元素在迭代器的第一个和最后一个之间, $middle true。

$last boolean true if the repeated element is last in the iterator. $last boolean如果重复元素在迭代器中位于最后, $last true。

$even boolean true if the iterator position $index is even (otherwise false). $even boolean如果迭代器位置$ index为偶数,则为true (否则为false)。

$odd boolean true if the iterator position $index is odd (otherwise false). $odd boolean如果迭代器位置$ index为奇数(否则为false),则为true。

您可以创建一个公共控制器并在该控制器中定义一个范围变量,然后可以将公共控制器的引用提供给要获取此变量的控制器文件。

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

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