0
코드를 볼 수 있듯이 서비스를 사용하여 값을 가져옵니다. AnotherCtrl
에 서비스를 사용하여 레코드를 가져온 다음 $scope
에 레코드를 하나 더 추가하고 해당 값도 MyCtrl
에서 변경되지만 은 MyCtrl
으로 업데이트하고 싶지 않습니다.angularjs에서 서비스를 사용할 때 모든 컨트롤러에서 값이 변경됩니다.
var app = angular.module("MyApp", []);
app.factory("UserService", function() {
var users = ["Peter", "Daniel", "Nina"];
return {
all: function() {
return users;
},
first: function() {
return users[0];
}
};
});
app.controller("MyCtrl", function($scope, UserService) {
debugger;
$scope.users = UserService.all();
});
app.controller("AnotherCtrl", function($scope, UserService) {
debugger;
$scope.firstUser = UserService.all();
$scope.firstUser.push('chirag');
});
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<body ng-app="MyApp">
<div ng-controller="MyCtrl">
<ul ng-repeat="user in users">
<li>{{user}}</li>
</ul>
<div class="nested" ng-controller="AnotherCtrl">
First user: {{firstUser}}
</div>
</div>
</body>
감사를 복사, 그것은 작업, 당신은 당신이 환영합니다 내 하루 –
을 저장 :) 케탄 – adutu