Css angularjs中的类型错误
我试图根据某些条件将列表中的某些值标记为字体红色和rest默认值。当我试图为列表中的特定数据分配布尔值时,我遇到了“TypeError:无法分配到123的只读属性'match'; 我的代码是:Css angularjs中的类型错误,css,angularjs,Css,Angularjs,我试图根据某些条件将列表中的某些值标记为字体红色和rest默认值。当我试图为列表中的特定数据分配布尔值时,我遇到了“TypeError:无法分配到123的只读属性'match'; 我的代码是: angular.forEach($scope.searchResults,函数(值,索引){ //填充搜索结果 $scope.searchResults[index].name=“ABC”; $scope.searchResults[index].linkedRecords=[]; if(//检查某些条
angular.forEach($scope.searchResults,函数(值,索引){
//填充搜索结果
$scope.searchResults[index].name=“ABC”;
$scope.searchResults[index].linkedRecords=[];
if(//检查某些条件){
$scope.searchResults[index].linkedRecords[i]=“123”;
$scope.searchResults[index].linkedRecords[i].match=true;
}
});
{{source}}
{{source}}
您将属性的值设置为123
,然后尝试访问名为'match'
的属性
$scope.searchResults[index].linkedRecords[i] = 123;
此时,$scope.searchResults[index].linkedRecords[i]
的值为123
这意味着这条线:
$scope.searchResults[index].linkedRecords[i].match=true;
相当于:
(123).match = true;
这将不起作用,因为数字类型是不可变的,这意味着您不能在其直接对象表示上添加或修改属性
您可能希望在对象内包装或装箱您的号码。然后您可以向该对象添加其他属性
$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>
{{source.value}}
{{source.value}}
知道如何在html中实现这一点吗?我需要设置一些内容,并使列表中的项目显示为红色。您能提供更多的代码吗?可能是$scope.searchResults的示例数据集?将数字包装在对象中。$scope.searchResults[index].linkedRecords[i]={value:123,match:true}
然后在HTML中进行source.match
和source.value
。@Dan Prince:非常感谢…:)