I am using "controlleras" syntax. I have main controller with ng-view, with
each child page having its own child controller. I wish to set the property
of main controller through JS from my child controller pages. I am using
this as reference http://codetunnel.io/angularjs-controller-as-or-scope/
In my below code i have two textboxes one in main controller and other in
child. If i type in either of the textboxes, both textboxes are updating
simultaneously and working as expected. But i want to set the value of
"main.data.message" from a JS function. There are two ways which i found
$scope.$parent.main.data.message or simply $scope.main.data.message. But i
do not want to inject $scope, i wanted to use controller syntax. How it is
working inside my child view html pages as expression {{pobj.property}} and
not inside controller as "var a = pobj.propoerty" as according to the below
url they are saying the childscope should have parentscope properties
https://github.com/angular/angular.js/wiki/Understanding-Scopes
Main.html
<html ng-controller="IndexController as main">
<body>
<input type="text" ng-model="main.data.message"/>
{{main.data.message}}
<section ng-view ></section>
</body>
</html>
Main controller
var POSapp = angular.module('POS', ['ngRoute', 'blockUI', 'ngSanitize',
'ngGrid', 'ngAnimate', 'Directives', 'Translation', 'Authentication']);
POSapp.controller("IndexController", [ '$location', function ( $location ) {
var vm = this;
vm.initializeController = function () {
vm.data = {"message":"testing"};
}
}]);
child pages
<div>
<input type="text" ng-model="main.data.message" />
{{main.data.message}}
</div>
POSapp.controller("SalesController", SalesController);
function SalesController()
{
var vm = this;
console.log($scope.main);
console.log($scope.$parent.main);
// below are erroring out
console.log($scope.$parent.main);
console.log($scope.$parent.main);
console.log($scope.$parent.main);
}
--
You received this message because you are subscribed to the Google Groups
"AngularJS" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To post to this group, send email to [email protected].
Visit this group at http://groups.google.com/group/angular.
For more options, visit https://groups.google.com/d/optout.