简体   繁体   English

ng-repeat-弹出后,您将无法按

[英]ng-repeat - Once you pop, you can't push

I have a small issue in Angular where I have a table, the rows of which are bound to an array inside of a controller, like so: 我在Angular中有一个小问题,我有一个表,该表的行绑定到控制器内部的数组,如下所示:

<div ng-controller="MyCtrl">

    <input type="button" value="Pop" ng-click="popone()" />
    <input type="button" value="Push" ng-click="pushone()" />

    <table>
        <tr ng-repeat="n in myLiveArray">
            <td>{{n.a}}</td>
            <td>{{n.b}}</td>
        </tr>
    </table>
</div>

And here is the controller: 这是控制器:

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

function MyCtrl($scope) {
    var myArray = [];

    var a = {a:2,b:0};
    var b = {a:3,b:2};
    var c = {a:4,b:2};
    var d = {a:5,b:3};
    var e = {a:6,b:4};

    myArray.push(a);
    myArray.push(b);
    myArray.push(c);
    myArray.push(d);
    myArray.push(e);

    $scope.myArray = myArray;

    var myLiveArray = [];
    $scope.myLiveArray = myLiveArray;

    for(var i = 0; i < myArray.length; i++){
        $scope.myLiveArray.push($scope.myArray[i]);
    }

    $scope.popone = function(){
        $scope.myLiveArray.pop();
        $scope.count--;
    }

    $scope.pushone = function(){
        $scope.myLiveArray.push($scope.myArray[$scope.count]);
        $scope.count++;
    }
}

A JSFiddle is here . 一个JSFiddle在这里

The issue is that when objects are popped off the array, the binding works as expected and the DOM is updated, but when you try to push the same object back onto the array then this doesn't work. 问题是,当对象从数组中弹出时,绑定将按预期方式工作并且DOM被更新,但是当您尝试将同一对象推回到数组上时,这将不起作用。

If I bind directly to an array of value types, I can push and pop as I please and the table updates both ways, but in this scenario I can only pop. 如果直接绑定到值类型数组,则可以随意按入和弹出,并且表会同时更新,但是在这种情况下,我只能弹出。

Can anybody shed some light on why this is? 有人可以阐明为什么会这样吗? I'm still very new at Angular, cheers. 我在Angular还是很新,很高兴。

Your code does work, it's just that you're not pushing anything with an expected value to the array because count is undefined, so the array is full of "undefined"s. 您的代码确实有效,只是您没有将任何具有期望值的东西推入数组,因为count是未定义的,因此数组中充满了“ undefined”。 If you push and then click pop, you'll notice nothing seems to happen, when actually, it's popping off one of the undefineds. 如果按下然后单击“弹出”,您会发现似乎没有任何反应,实际上,它是从未定义的其中之一弹出的。 If you add some alerts in the pop and push functions you can see this happening. 如果您在弹出和推送功能中添加了一些警报,则可以看到这种情况。

add this to pushone: 将此添加到pushone:

alert($scope.count+" "+$scope.myArray[$scope.count]);

returns: undefined undefined on every click push. 返回:undefined每次单击时均未定义。

When you actually push something there it works as expected. 当您实际推动某物时,它会按预期工作。

initialize count, and make sure it stays within bounds and you should be good. 初始化计数,并确保它不超出范围,并且您应该不错。

Console log is your friend :) 控制台日志是您的朋友:)

This is your jsfiddle with some logs added: http://jsfiddle.net/HB7LU/3515/ 这是您的jsfiddle,其中添加了一些日志: http : //jsfiddle.net/HB7LU/3515/

    console.log($scope.count);
    console.log($scope.myArray);
    console.log($scope.myLiveArray);

open up your console and click push. 打开控制台,然后单击“推送”。 You can see that your counter is NaN. 您可以看到您的柜台是NaN。 I think it's because it's not initialized. 我认为这是因为未初始化。 You can start by initializing it to 4 and continue from there, think about the edge cases. 您可以先将其初始化为4,然后再继续考虑边缘情况。

Try setting $scope.count = $scope.myLiveArray.length at the very end of your controller definition. 尝试在控制器定义的最后设置$ scope.count = $ scope.myLiveArray.length。

  $scope.pushone = function(){
        $scope.myLiveArray.push($scope.myArray[$scope.count]);

    }

   $scope.count = $scope.myLiveArray.length;

No need to manually set it anywhere now (I removed it from the pushone method). 现在无需在任何地方进行手动设置(我已从pushone方法中将其删除)。 Angular should automatically update it for you Angular应该会自动为您更新

You shouldn't increment and decrement $scope.count . 您不应该增加和减少$scope.count

This is the better approach: 这是更好的方法:

$scope.myLiveArray.push($scope.myArray[$scope.myLiveArray.length]);

http://jsfiddle.net/sW8G7/1/ http://jsfiddle.net/sW8G7/1/

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

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