1

我是 angularjs 的新手。面临如下问题我的标记就像:

<div class="data-list">
    <ul ng-repeat="client in clients | orderBy:orderProp | filter:query">
        <li><a ng-click="update(client)"  data={{client.id}} href="#">{{client.name}}</a></li>
    </ul>
</div>
<div class="clientId">
    {{clientId}}
</div>

我的控制器就像:

function ClientListCtrl($scope, $http) {
    $http.get('/dummy-data/clients.json', {
        cache: false
    }).success(function (data) {
        $scope.clients = data.clients;
    });

    $scope.activeClient = {};
    $scope.update = function (selectedClient) {
        $scope.activeClient = angular.copy(selectedClient);
        console.log($scope.activeClient);
    }

    console.log("after click");
    console.log($scope.activeClient);
    // for sorting list 
    $scope.orderProp = '-numberOfUsers';
}

我希望当用户单击“客户端名称”时{{clientId}}会更新,但范围会自行更新。并返回{}(空白对象)。

单击客户端名称后如何获取 {{clientId}}?

提前致谢。

4

2 回答 2

0

您的控制器没有任何类似的属性,clientId并且您ng-repeat的范围仅限于<ul>标签。

改变

<div class="clientId">
   {{clientID}}
</div>

<div class="clientId">
   {{activeClient}}
</div>
于 2013-05-14T14:21:22.437 回答
0

你必须替换{{clientId}}{{activeClient.id}}

我根据你的代码做了一个简单的例子。

HTML

<div ng-app="myApp">
    <div ng-controller="ClientListCtrl">
        <ul ng-repeat="client in clients">
            <li><a ng-click="update(client)" href="#">{{client.name}}</a></li>
        </ul>
        <div class="clientId"><br/>
            Client id: {{activeClient.id}}
        </div>
    </div>
</div> 

JavaScript

var myApp = angular.module('myApp',[]);

function ClientListCtrl($scope, $http) {
    $scope.clients = [
        {"id":1, "name": "client1"},
        {"id":2, "name": "client2"},
        {"id":3, "name": "client3"},
        {"id":4, "name": "client4"},
        {"id":5, "name": "client5"},
        {"id":6, "name": "client6"}
    ];

    $scope.activeClient = {};
    $scope.update = function(selectedClient) {
        $scope.activeClient = angular.copy(selectedClient);
    }
}

看到这个jsfiddle

于 2013-05-14T14:22:44.887 回答