search

Home  >  Q&A  >  body text

angular.js - Regarding the issue of automatic binding after data assignment in scope

I need to copy the data in the scope, such as $scope.a to $scope.b. I found that when $scope.b is changed, $scope.a will automatically change, but I don’t want the two of them to automatically change. Data binding. I use var c = $scope.a;$scope.b = c; data will be automatically bound between them. What should I do to prevent them from being automatically bound?

PHP中文网PHP中文网2778 days ago558

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