我可以在ng-repeat中设置一个变量吗?

时间:2015-05-09 13:54:46

标签: javascript html angularjs angularjs-ng-repeat angularjs-ng-click

我有一个应用程序有两个表单,即按钮列表和一个带有标签列表的表单。我选择一个人,选择一个电话,然后在第三个表格中显示电话及其相关人员(位置)列表。是否可以在ng-click块内设置ng-repeat的变量?我尝试将变量_person设置为等于按钮文本,这将是{{person}},但是当我在下一个表单上打印时,_person似乎没有被设置为任何东西。我也不确定我是否在第一个ng-init中正确使用了<div>,我应该使用ng-model吗?

<div ng-init="showSelectUser=true; _person=''">
<div class="selectUser" ng-show="showSelectUser">
    <h2>Who are you?</h1>
    <ul ng-click="showSelectUser=false; showDeviceForm=true;">
      <li ng-repeat="person in people">
        <a href="#" role="button" ng-click="_person={{person}}">{{person}}</a>
      </li>
    </ul>
  </div>

  <div class="selectDevice" ng-show="showDeviceForm" ng-click="showDeviceForm=false; showDeviceList=true">
    <p>person: {{_person}}</p>
    <h2>Which phone?</h2>
    <ul>
      <li ng-repeat="device in devices">
        <a class="btn btn-default" href="#" role="button" ng-click="device.location=_person">{{device.name}}</a>
      </li>
    </ul>
  </div>

  <div class="devicesView" ng-show="showDeviceList">
    <ul>
      <li ng-repeat="device in devices">
        <h3>{{device.name}}</h3>
        <h4 class="deviceLocation">{{device.location}}</h4>
      </li>
    </ul>
  </div>
</div>



angular.module('devicesApp')
  .controller('MainCtrl', function ($scope) {
    $scope.devices = [
        {name: 'iPhone 4', location: 'Desk'},
        {name: 'iPhone 5', location: 'Desk'},
        {name: 'iPhone 6', location: 'Desk'},
    ];

    $scope.people = [
        'John',
        'Scott',
        'Adam'
    ];
});

1 个答案:

答案 0 :(得分:4)

ngRepeat指令创建一个新范围,因此如果你有ng-click="device.location=_person",那么device模型将在该范围内创建(如果它不存在)。因此,请确保父作用域中已存在device,例如在控制器中设置$scope.device = {}

_person未定义,因为它是在ng-repeat="person in people"内部范围内定义的。目前,您的ng-click会将每个设备位置设置为undefined

一般情况下,在ngClicks中使用表达式而不是执行ng-click="setPerson()"之类的操作并在控制器中使用$scope.setPerson = function () { ... }时,会有很多范围继承问题。

请用更高级别的术语说明你想做什么,我会更新我的答案。

修改

这样的事情对我来说更合乎逻辑。但是,我认为您将遇到很多UX问题,因为用户在单击其中一个列表项后无法更改其选择。 请注意,如果将业务逻辑放在JS文件而不是模板中,则更容易测试。

 <div ng-app="devicesApp" ng-controller="MainCtrl">
    <div class="selectUser" ng-show="step === 'step1'">
        <h2>Who are you?</h2>
        <ul>
            <li ng-repeat="person in people">
                <a href="#" role="button" ng-click="selectPerson(person)">{{person}}</a>
            </li>
        </ul>
    </div>
    <div class="selectDevice" ng-show="step === 'step2'">
        <p>person: {{selected.person}}</p>
         <h2>Which phone?</h2>

        <ul>
            <li ng-repeat="device in devices"> <a class="btn btn-default" href="#" role="button" ng-click="selectDevice(device)">{{device.name}}</a>

            </li>
        </ul>
    </div>
    <div class="devicesView" ng-show="step === 'step3'">
        <ul>
            <li ng-repeat="device in devices">
                 <h3>{{device.name}}</h3>

                 <h4 class="deviceLocation">{{device.location}}</h4>

            </li>
        </ul>
    </div>
</div>

在JS文件中:

angular.module('devicesApp', []).controller('MainCtrl', function ($scope) {
    $scope.devices = [{
        name: 'iPhone 4',
        location: 'Desk'
    }, {
        name: 'iPhone 5',
        location: 'Desk'
    }, {
        name: 'iPhone 6',
        location: 'Desk'
    }, ];

    $scope.people = [
        'John',
        'Scott',
        'Adam'];

    $scope.selected = {};
    $scope.step = 'step1';

    $scope.selectPerson = function (person) {
        $scope.selected.person = person;
        $scope.step = 'step2';
    };

    $scope.selectDevice = function (device) {
        device.location = $scope.selected.person;
        $scope.step = 'step3';
    }
});

请参阅this JSFiddle