Lodash가 findWhere를 사용하여 객체를 가져 오는 이유를 이해할 수 있도록 도와 줄 수 있습니까? 전체 변수를 범위에 바인딩하고 사용자 작업에 따라 동적으로 변경할 수있는 참조 사본이 있습니다. copy 연산자를 사용하여 수행 할 때와 동일한 작업으로 원본 개체를 업데이트하지 못합니다. 나는 plunker 을 뒀다 http://plnkr.co/edit/Phy5xQnGBYsBnenMQHOd?p=previewLodash 이해 - 자바 스크립트의 찾기 및 복사 연산자
HTML 코드 :
<html ng-app="angularjs-starter">
<head lang="en">
<meta charset="utf-8" />
<title>Custom Plunker for service response data binding</title>
<script data-require="[email protected]" data-semver="2.4.1" src="http://cdnjs.cloudflare.com/ajax/libs/lodash.js/2.4.1/lodash.js"></script>
<script src="//ajax.googleapis.com/ajax/libs/angularjs/1.0.3/angular.min.js"></script>
<link rel="stylesheet" href="style.css" />
<script src="app.js"></script>
</head>
<body ng-controller="MainCtrl">
<input type="text" data-ng-model="inputModel" />
<input type="text" data-ng-model="inputModel2.desc" />
<button data-ng-click="btnClick()">Click me and check console</button>
</body>
</html>
JS 코드 :
var app = angular.module('angularjs-starter', []);
app.controller('MainCtrl', function($scope,$http) {
var someServiceResponse = [{"code":"1","desc":"apple"},{"code":"2","desc":"orange"}];
$scope.inputModel = someServiceResponse[0].desc;
$scope.inputModel2 = _.findWhere(someServiceResponse,{'code':'2'});
$scope.btnClick = function(){
console.log($scope.inputModel);//Outputs the changed value in the text box1
console.log($scope.inputModel2.desc);//Outputs the changed value in the text box2
console.log(someServiceResponse[0].desc); // Still shows the old value from the service response - always shows apple
console.log(someServiceResponse[1].desc); // shows updated value from second text box
};
});
나를 위해 마지막 두 console.log는 항상 사과와 오렌지색을 표시합니다. – Absor
Absor - 사과! 내 잘못이야. 나는 plunker뿐만 아니라 질문을 업데이트했다. – WiseWins
inputModel은 문자열에 대한 참조이며 업데이트 된 경우 참조가 새 문자열로 설정됩니다. inputModel2는 객체에 대한 참조이며 내부에서 desc 문자열을 업데이트합니다. – Absor