1
我有以下HTML結構:angularjs嵌套控制器 - 形式不確定
<body ng-controller="checkoutController">
<div class="main clearfix" ng-controller="abroadCourseController">
//html useless information
<form name="checkout_form" ng-submit="submitForm()" novalidate>
<div validate-section="checkoutInfo" ng-show="stepOne">
//fields
<div class="confirmationBox">
<button type="button" ng-click="displayPaymentMethods()">SHOW PAYMENT METHODS</button>
</div>
</div>
<div validate-section="paymentAbroadCourseB2C" ng-show="stepTwo" >
//fields
<div class="confirmationBox">
<button type="button" ng-click="submitForm()">FINISH</button>
</div>
</div>
</form>
</div>
</body>
及以下JS:
var myApp = angular.module('myApp',[]);
myApp.controller('checkoutController', function ($scope) {
$scope.submitForm = function(){
$scope.stepOne = true;
$scope.stepTwo = false;
alert($scope.checkout_form);
alert('oi');
};
});
myApp.controller('abroadCourseController', function ($scope) {
$scope.stepOne = true;
$scope.stepTwo = false;
$scope.displayPaymentMethods = function(){
$scope.stepOne = false;
$scope.stepTwo = true;
alert($scope.checkout_form);
alert('oi');
};
});
基本上是我需要的是有通過父控制器上checkout_form訪問,但是,它是未定義的。有沒有辦法實現這一點?
這裏有一個的jsfiddle:http://jsfiddle.net/thm259o7/
可以使用$ rootScope爲 –
你說是父控制器上未定義 - 孩子控制器呢?它也沒有定義嗎? PS不使用警報,因爲它會凍結線程並導致大量問題,請改用console.log。實際上使用警報可能是你的問題 – danday74
@ danday74 console.log在jsfiddle –