gpt4 book ai didi

angularjs - 使用另一个 $scope 变量显示 $scope 变量

转载 作者:行者123 更新时间:2023-12-01 23:00:27 26 4
gpt4 key购买 nike

显示作用域变量,类似于 Angularjs 中的 ng-repeat 元素

这些是我的范围变量

$scope.published = true;
$scope.count = 3;

我还有一个名为 labels 的数组

$scope.labels = ['published', 'count'];

在 View 中,我想以标签名称 - 标签值的形式查看这些数据。

<div ng-repeat="label in labels">
{{label}} - {{Here I want the value from the Scope variable}}
</div>

有人可以帮助我如何在这种情况下访问作用域变量吗?

最佳答案

使用this标识符和bracket notation property accessors :

<div ng-repeat="label in labels">
{{label}} - {{this[label]}}
</div>

来自文档:

It is possible to access the context object using the identifier this and the locals object using the identifier $locals.

欲了解更多信息,请参阅

演示

angular.module("app",[])
.controller("ctrl", function($scope) {
$scope.published = true;
$scope.count = 3;
$scope.labels = ['published', 'count'];
})
<script src="//unpkg.com/angular/angular.js"></script>
<body ng-app="app" ng-controller="ctrl">
<div ng-repeat="label in labels">
{{label}} - {{this[label]}}
</div>
</body>

关于angularjs - 使用另一个 $scope 变量显示 $scope 变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60219626/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com