1. 程式人生 > >angularjs 巢狀控制器,子控制器訪問父控制器

angularjs 巢狀控制器,子控制器訪問父控制器

<script src="angular-1.3.0.js"></script>
<div ng-controller="OparentController as pCtrl">
    <p>直接訪問父控制器的值:{{user}}</p>
    <div ng-controller="OchildController as cCtrl">
        <p>巢狀如何訪問。。父控制器的值:失敗:<input ng-model="pUser"/>成功:<input ng-model="obj2.pUser"/></p>
        <p>子控制器的值:{{user}}</p>
        <button ng-click="modiV()">clk:子控制器內修改父控制器內(作用域)的值</div>
    </div>
</div>
<script>
    var
a10086=angular.module('a10086',[]); a10086.controller('OparentController',function($scope){ $scope.user='parent_李四'; $scope.msg={a:'parent_李四',b:4,c:[1,34]}; }).controller('OchildController',function($scope){ $scope.user='child_張三'; $scope.pUser=$scope.msg.a; $scope.obj2
={ pUser:$scope.msg.a }; $scope.modiV=function(){ $scope.msg.a="parent_李四 is modify"; $scope.obj2.pUser="parent_李四 is modify---obj"; console.log('子控制沒有msg,才順著原型鏈修改了:',$scope.msg, $scope.pUser); } }); </script>