부모 컨트롤러의 변수를 상속할 수 있습니까?
코드는 다음과 같습니다.
function ParentCtrl($scope) {
$scope.people = ["Tom", "Dick", "Harry"];
$scope.count = $scope.people.length;
}
function ChildCtrl($scope) {
$scope.parentpeople = ParentCtrl.people //this is what I would like to do ideally
}
나는 한 앵글을 둥지로 만들고 있다.controller다른 것들의 내부.첫 번째 변수를 전달하고 싶습니다.controller두 번째 것까지.이거 할 줄 아는 사람 있어요?
메모
나는 이런 것을 할 수 없다.
ChildCtrl.prototype = new ParentCtrl();
이 파일을 덮어쓸 것이기 때문에people의 특성ChildCtrl.
기본적으로 하위 범위는 상위 범위에서 프로토타입으로 상속되므로(범위 참조), 하위 범위에서 상위 컨트롤러의 $scope 속성에 이미 액세스할 수 있습니다.증명하려면:
function ChildCtrl($scope) {
alert($scope.people)
}
당신은 잘못 알고 있어요.컨트롤러 상속과 스코프 상속을 혼합하고 있으며, 각에서 서로 다르고 느슨하게 결합되어 있습니다.JS.
실제로 원하는 것은 다음과 같습니다.
function ParentCtrl($scope) {
$scope.people = ["Tom", "Dick", "Harry"];
$scope.count = $scope.people.length;
}
function ChildCtrl($scope) {
$scope.parentpeople = $scope.$parent.people;
}
그리고 이 경우엔 효과가 있을 것입니다.
<div ng-controller="ParentCtrl">
<div ng-controller="ChildCtrl">
</div>
</div>
그러나 Mark와 ganaraj가 위에서 알아차렸듯이, $scope라는 당신의 재산에 접근할 수 있기 때문에 이것은 의미가 없다.ParentCtrl과 ChildCtrl 양쪽에서 온 사람들.
컨트롤러를 서로 상속하려면 컨트롤러 기능 자체의 프로토타입 상속을 사용해야 합니다.
$scope 상속은 ng-controller를 사용하여 컨트롤러를 참조하는 위치에 따라 달라집니다.
이런 게 있다면
<div ng-controller="ParentController">
<div ng-controller="ChildController">
</div>
</div>
그러면 하위 컨트롤러가 상위 컨트롤러의 속성을 상속합니다.
주의: 자녀 컨트롤러를 html의 직계 자녀에 정의할 필요는 없습니다.안에 있는 어떤 아이도 될 수 있어요.
또, DOM 에 의해서 임의의 컨트롤러의 스코프를 취득할 수도 있습니다.
$needleScope = angular.element(aDomElement).scope()
jQuery 사용:
$needleScope = $('#aDomElementId').scope()
또는 문서의 모든 범위를 가져옵니다.
$allScopes = $('.ng-scope').scope()
도움이 될지도 몰라!!!
Scope는 컨트롤러와 뷰를 연결하는 특별한 JavaScript 개체입니다.범위에 모델 데이터가 포함되어 있습니다.컨트롤러에서는 $scope 객체를 통해 모델 데이터에 액세스합니다.
<script>
var mainApp = angular.module("mainApp", []);
mainApp.controller("shapeController", function($scope) {
$scope.message = "In shape controller";
$scope.type = "Shape";
});
</script>
범위 상속 범위는 컨트롤러에 따라 다릅니다.중첩된 컨트롤러를 정의하면 하위 컨트롤러는 상위 컨트롤러의 범위를 상속합니다.
<script>
var mainApp = angular.module("mainApp", []);
mainApp.controller("shapeController", function($scope) {
$scope.message = "In shape controller";
$scope.type = "Shape";
});
mainApp.controller("circleController", function($scope) {
$scope.message = "In circle controller";
});
</script>
아래에 제시된 대로 실제 사례를 제시합니다.
<html>
<head>
<title>Angular JS Forms</title>
</head>
<body>
<h2>AngularJS Sample Application</h2>
<div ng-app="mainApp" ng-controller="shapeController">
<p>{{message}} <br/> {{type}} </p>
<div ng-controller="circleController">
<p>{{message}} <br/> {{type}} </p>
</div>
<div ng-controller="squareController">
<p>{{message}} <br/> {{type}} </p>
</div>
</div>
<script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.2.15/angular.min.js"></script>
<script>
var mainApp = angular.module("mainApp", []);
mainApp.controller("shapeController", function($scope) {
$scope.message = "In shape controller";
$scope.type = "Shape";
});
mainApp.controller("circleController", function($scope) {
$scope.message = "In circle controller";
});
mainApp.controller("squareController", function($scope) {
$scope.message = "In square controller";
$scope.type = "Square";
});
</script>
</body>
</html>
언급URL : https://stackoverflow.com/questions/12008150/can-i-inherit-a-parent-controllers-variables
'programing' 카테고리의 다른 글
| AngularJS: ng-read only (0) | 2023.03.10 |
|---|---|
| 스프링 보안 CORS 필터 (0) | 2023.03.10 |
| 페이지를 갱신하지 않고 URI를 변경하려면 어떻게 해야 합니까? (0) | 2023.03.10 |
| 워드프레스 테마에서 쇼트코드 사용 (0) | 2023.03.10 |
| JSON 구문 분석 오류: java.time 인스턴스를 생성할 수 없습니다.LocalDate: String 값에서 역직렬화할 문자열 인수 생성자/공장 메서드가 없습니다. (0) | 2023.03.10 |