각도에서 부모 범위 변수 업데이트
하나는 다른 컨트롤러 안에 포장되어 있습니다. 이제 자식 범위가 부모 범위에서 속성을 상속하지만 부모 범위 변수를 업데이트하는 방법이 있습니까? 지금까지 나는 확실한 해결책을 찾지 못했습니다.
내 상황에서는 양식 내에 캘린더 컨트롤러가 있습니다. 제출할 때 양식이 시작 및 종료 날짜를 갖도록 부모 범위 (양식)에서 시작 및 종료 날짜를 업데이트하고 싶습니다.
부모 범위에서 기본 요소가 아닌 개체를 사용해야하고 자식 범위에서 직접 개체를 업데이트 할 수 있습니다.
부모의:
app.controller('ctrlParent',function($scope){
$scope.parentprimitive = "someprimitive";
$scope.parentobj = {};
$scope.parentobj.parentproperty = "someproperty";
});
아이:
app.controller('ctrlChild',function($scope){
$scope.parentprimitive = "this will NOT modify the parent"; //new child scope variable
$scope.parentobj.parentproperty = "this WILL modify the parent";
});
실무 데모 : http://jsfiddle.net/sh0ber/xxNxj/
AngularJS에서 스코프 프로토 타입 / 프로토 타입 상속의 뉘앙스는 무엇입니까?를 참조하십시오 .
이 작업을 수행하고 $scope.$parent
변수를 사용하지 않는 다른 방법이 있습니다.
부모 범위의 값을 변경하는 방법을 준비하고 자식 범위에서 사용하십시오. 이처럼 :
app.controller('ctrlParent',function($scope) {
$scope.simpleValue = 'x';
$scope.changeSimpleValue = function(newVal) {
$scope.simpleValue = newVal;
};
});
app.controller('ctrlChild',function($scope){
$scope.changeSimpleValue('y');
});
또한 작동하며 값 변경을보다 강력하게 제어 할 수 있습니다.
그런 다음 HTML과 같이 메소드를 호출 할 수도 있습니다 <a ng-click="changeSimpleValue('y')" href="#">click me!</a>
.
이것은 또한 작동합니다 (그러나 이것이 모범 사례를 따르는 지 여부는 확실하지 않습니다)
app.controller('ctrlParent',function($scope) {
$scope.simpleValue = 'x';
});
app.controller('ctrlChild',function($scope){
$scope.$parent.simpleValue = 'y';
});
기본 속성을 범위에 할당하면 상위 범위에 이름이 같은 속성이 있더라도 항상 범위에 해당됩니다 (아마도 즉시 생성됨). 이것은 디자인 결정이며 좋은 IMHO입니다.
상위 범위의 일부 기본 요소 (int, boolean, string)를 뷰에서 변경해야하는 경우 뷰에서 해당 범위의 다른 객체의 속성이어야하므로 할당은 다음과 같습니다.
<a ng-click="viewData.myAttr = 4">Click me!</a>
그리고 그것은 차례로 :
- get the
viewData
object from whatever scope it is defined in - assign 4 to its
myAttr
attribute.
For accessing variables declared in the parent, we should use $parent in child controller or template file
In controller
$scope.$parent.varaiable_name
In html template
ng-model="$parent.varaiable_name"
참고URL : https://stackoverflow.com/questions/16928341/update-parent-scope-variable-in-angular
'development' 카테고리의 다른 글
리눅스에서 프로세스의 스레드 수를 어떻게 모니터링 할 수 있습니까? (0) | 2020.07.25 |
---|---|
Java gradle 프로젝트를 만드는 방법 (0) | 2020.07.25 |
python pandas : 열 A에서 중복을 제거하고 열 B에서 가장 높은 값을 유지 (0) | 2020.07.24 |
기본값으로 false를 사용하여 MySQL에서 부울 열을 작성 하시겠습니까? (0) | 2020.07.24 |
그룹화 된 테이블 뷰 셀의 배경 / 테두리 색상을 사용자 정의하는 방법은 무엇입니까? (0) | 2020.07.24 |