$$ hashKey在Angular中的push()新对象时不生成新值

时间:2016-05-05 12:04:37

标签: javascript arrays angularjs

我发现生成新的$ hashKey有问题。 在这段代码中:

this.list = '[{
    "body": "asdf",
    "tag": "resolved",
    "time": "2147483647",
    "id": "51"
}, {
    "body": "asdf",
    "tag": "undone",
    "time": "2147483647",
    "id": "138"
}, {
    "body": "asdf",
    "tag": "undone",
    "time": "2147483647",
    "id": "139"
}]';

this.addTask = function(body) {

    newObj.body = body;
    newObj.tag = 'undone';
    newObj.time = Date.now();

    this.list.push(newObj);
}

当我添加第二个对象时,它的$$ hashKey相同。我在ng-repeat this.list时遇到错误。

如何手动递增或有简单的解决方案?

1 个答案:

答案 0 :(得分:1)

试试这个

this.addTask = function(body) {
    var newObj = { body : body, tag :'undone', time: Date.now() };    
    this.list.push(newObj);
}