angularjs中的TypeError

时间:2015-11-04 19:44:00

标签: css angularjs

我试图将列表中的某些值标记为字体红色,并根据某些条件将其默认为。当我尝试为列表中的特定数据分配布尔值时,我得到“TypeError:无法分配给只读属性'匹配'123; 我的代码:

angular.forEach($scope.searchResults, function (value, index) {

  //populate searchResults
   $scope.searchResults[index].name = "ABC";
  
    $scope.searchResults[index].linkedRecords = [];
  if(//check some condition){
    $scope.searchResults[index].linkedRecords[i] ="123"; 
    $scope.searchResults[index].linkedRecords[i].match=true;
  }
    
  
  });
 <tr data-ng-repeat="searchResult in searchResults ">
   
   <td >
            <span data-ng-repeat="source in searchResult.linkedRecords" >
                <span ng-if="!source.match">{{ source }}</span>
                <span ng-if="source.match" style="color: red">{{ source }}</span>
                <br>
            </span></td>
   </tr>

任何想法如何在html中使这个工作?我需要为每个项目设置一些内容,并使列表中的这些项目显示为红色。

1 个答案:

答案 0 :(得分:0)

您要将该属性设置为值123,然后您尝试访问名为'match'的属性。

$scope.searchResults[index].linkedRecords[i] = 123;

此时$scope.searchResults[index].linkedRecords[i]的值为123

这意味着这一行:

$scope.searchResults[index].linkedRecords[i].match=true;

相当于:

(123).match = true;

这不会起作用,因为Number类型是不可变的,这意味着你不能在它们的直接对象表示上添加或修改属性。

您可能希望在对象内包装您的号码。然后,您可以向该对象添加其他属性。

$scope.searchResults[index].linkedRecords[i] = { value: 123, match: true };

然后你的HTML会看起来像这样。

<span data-ng-repeat="source in searchResult.linkedRecords" >
  <span ng-if="!source.match">{{ source.value }}</span>
  <span ng-if="source.match" style="color: red">{{ source.value }}</span>
  <br>
</span>