从外部获取值ng-repeat in angular

Get value from outside ng-repeat in angular

本文关键字:in angular ng-repeat 获取 从外部      更新时间:2023-09-26

如何从ng-repeat外部获取值?这是我从json

获取值的函数
function($scope, $http) {
        $http.get('https://www.mywebsite.com/images/getimages.php').
          success(function(data, status, headers, config) {
            $scope.walls = data.walls;
            console.log($scope.walls);
         }).error(function(data, status, headers, config) {
            // called asynchronously if an error occurs
            // or server returns response with an error status.
         });

那么我有一个表:

<tr ng-repeat="wall in walls">
                <td>{{wall.id}}</td>
                <td><a href="#" ng-click="">{{wall.link}}</a></td>            
            </tr>

i将在表外的div中显示,因此在ng-repeat之外,{{墙。链接}}从td。我该怎么做呢?

您可以实现控制器功能来设置选定的墙壁对象:

function ($scope, $http) {
    $http.get('https://www.mywebsite.com/images/getimages.php')
        .success(function (data, status, headers, config) { /*...*/ })
        .error(function (data, status, headers, config) { /*...*/ });
    $scope.select = function (wall) {
        $scope.selectedWall = wall;
    };
}

和HTML中的

<table>
    <tr ng-repeat="wall in walls">
        <td>{{wall.id}}</td>
        <td>
            <a href="#" ng-click="select(wall)">{{wall.link}}</a>
        </td>
    </tr>
</table>
<div>{{selectedWall.link}}</div>