简体   繁体   English

AngularJS范围更新问题

[英]Angularjs scope update issue

I am new to angularjs. 我是angularjs的新手。 Facing a problem like below My markup is like: 面对下面的问题我的标记如下:

<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>

And my controller is like: 我的控制器就像:

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';
}

I want when user click "Client Name", {{clientId}} would be updated, but the scope updates it self. 我希望当用户单击“客户端名称”时, {{clientId}}会被更新,但是范围会自行更新。 And return {} (blank object). 并返回{} (空白对象)。

How I can get the {{clientId}} after clicking the clientName? 单击clientName后如何获取{{clientId}}?

Thanks in advance. 提前致谢。

You controller does not have any property like clientId and you ng-repeat is scoped only to the <ul> tags. 您的控制器没有诸如clientId类的任何属性,并且ng-repeat仅限于<ul>标记。

Change 更改

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

to

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

You have to replace {{clientId}} with {{activeClient.id}} 您必须将{{clientId}}替换为{{activeClient.id}}

I made a simple example based on your code. 我根据您的代码制作了一个简单的示例。

HTML 的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 的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);
    }
}

see this jsfiddle 看到这个 jsfiddle

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM