Home  >  Q&A  >  body text

angular.js - 关于scope中数据赋值后自动绑定的问题

我需要复制出scope中的数据,如$scope.a给$scope.b,发现改变$scope.b时候$scope.a也会自动改变,但是我不希望他们两个之间会自动数据绑定。我用var c = $scope.a;$scope.b = c;它们之间也会自动数据绑定,应该如何做才能让它们之间不会自动绑定呢?

PHP中文网PHP中文网2713 days ago494

reply all(2)I'll reply

  • 黄舟

    黄舟2017-05-15 17:00:01

    Use angular.copy

    reply
    0
  • PHP中文网

    PHP中文网2017-05-15 17:00:01

    C If it is an object, a and b actually point to the same object. Using angular.copy to make a copy can solve the problem, but you may need to review whether this design is reasonable. Data-driven development should ensure that the data is consistent and unique. .

    cIf it is a basic type, such as strings and numbers, the situation you mentioned should not happen

    reply
    0
  • Cancelreply