How to share the $scope variable of one controller with another in AngularJS?

You could use an Angular Service to share variable acrosss multiple controllers. angular.module(‘myApp’, []) .service(‘User’, function () { return {}; }) To share the data among independent controllers, Services can be used. Create a service with the data model that needs to be shared. Inject the service in the respective controllers. function ControllerA($scope, User) { … Read more

AngularJS passing data to bootstrap modal

I’d suggest you to pass the scope of your own controller instead of passing same controller again, by doing that you can remove the resolve also. var modalInstance = $uibModal.open({ templateUrl: ‘myModalContent.html’, scope: $scope, //passed current scope to the modal size: ‘lg’ }); Otherwise you need to create a new controller and assign that controller … Read more

Accessing inherited scope with Controller As approach

After researching, I came to the following realization: Controller-As approach is NOT a substitute for using $scope. Both have their place, and can/should be used together judiciously. $scope does exactly what the name implies: i.e. it defines ViewModel properties on the $scope. This works best for sharing scope with nested controllers that can use the … Read more

How do I inject a controller into another controller in AngularJS

If your intention is to get hold of already instantiated controller of another component and that if you are following component/directive based approach you can always require a controller (instance of a component) from a another component that follows a certain hierarchy. For example: //some container component that provides a wizard and transcludes the page … Read more

Update parent scope variable in AngularJS

You need to use an object (not a primitive) in the parent scope and then you will be able to update it directly from the child scope Parent: app.controller(‘ctrlParent’,function($scope){ $scope.parentprimitive = “someprimitive”; $scope.parentobj = {}; $scope.parentobj.parentproperty = “someproperty”; }); Child: app.controller(‘ctrlChild’,function($scope){ $scope.parentprimitive = “this will NOT modify the parent”; //new child scope variable $scope.parentobj.parentproperty = … Read more

AngularJS : The correct way of binding to a service properties

Consider some pros and cons of the second approach: 0 {{lastUpdated}} instead of {{timerData.lastUpdated}}, which could just as easily be {{timer.lastUpdated}}, which I might argue is more readable (but let’s not argue… I’m giving this point a neutral rating so you decide for yourself) +1 It may be convenient that the controller acts as a … Read more

Can an AngularJS controller inherit from another controller in the same module?

Yes, it can but you have to use the $controller service to instantiate the controller instead:- var app = angular.module(‘angularjs-starter’, []); app.controller(‘ParentCtrl’, function($scope) { // I’m the sibling, but want to act as parent }); app.controller(‘ChildCtrl’, function($scope, $controller) { $controller(‘ParentCtrl’, {$scope: $scope}); //This works });