How to access variables in a parent controller within a child controller

Source: Internet
Author: User

<div ng-controller= "Oparentcontroller as Pctrl" >
<p> direct access to the value of the parent controller:{{user}}</p>
<div ng-controller= "Ochildcontroller as Cctrl" >
<p> How to access the nested: Parent controller value: failed: <input ng-model= "Puser"/> Success: <input ng-model= "Obj2.puser"/></p>
<p> Sub-controller value:{{user}}</p>
<button ng-click= "Modiv ()" &GT;CLK: Modify the values within the parent controller (scope) within the child controller </div>
</div>
</div>
<script>
var a10086=angular.module (' a10086 ', []);
A10086.controller (' Oparentcontroller ', function ($scope) {
$scope. user= ' parent_ John Doe ';
$scope. Msg={a: ' Parent_ John Doe ', b:4,c:[1,34]};
}). controller (' Ochildcontroller ', function ($scope) {
$scope. user= ' child_ Zhang San ';
$scope. puser= $scope. msg.a;
$scope. obj2={
Puser: $scope. msg.a
};
$scope. Modiv=function () {
$scope. msg.a= "Parent_ John Doe is modify";
$scope. obj2.puser= "Parent_ John Doe is modify---obj";
Console.log (' sub-control does not have MSG, but modified along the prototype chain: ', $scope. MSG, $scope. Puser);
}
});
</script>

How to access variables in a parent controller within a child controller

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.