Questions tagged [angularjs-controller]

The AngularJS controller exposes the data that is to be displayed into the HTML view. It plays the essential role of the ModelView in the MVVM design pattern. The view is a part of the HTML template.

AngularJS promotes the MVVM Design pattern.

The angularjs controller exposes the data that is to be displayed into the html view. It plays the essential role of the ModelView in the MVVM design pattern. The view is a part of the HTML template.

When a Controller is attached to the DOM via the ng-controller directive, Angular will instantiate a new Controller object, using the specified Controller's constructor function. A new child scope will be available as an injectable parameter to the Controller's constructor function as $scope.

ngController
The ngController directive attaches a controller class to the view. This is a key aspect of how angular supports the principles behind the Model-View-Controller design pattern.

Reference:
https://docs.angularjs.org/guide/controller
https://docs.angularjs.org/api/ng/directive/ngController

1243 questions
333
votes
16 answers

AngularJS: How can I pass variables between controllers?

I have two Angular controllers: function Ctrl1($scope) { $scope.prop1 = "First"; } function Ctrl2($scope) { $scope.prop2 = "Second"; $scope.both = Ctrl1.prop1 + $scope.prop2; //This is what I would like to do ideally } I can't use…
dopatraman
  • 13,416
  • 29
  • 90
  • 154
219
votes
8 answers

How do I use $rootScope in Angular to store variables?

How do I use $rootScope to store variables in a controller I want to later access in another controller? For example: angular.module('myApp').controller('myCtrl', function($scope) { var a = //something in the scope //put it in the root…
trysis
  • 8,086
  • 17
  • 51
  • 80
201
votes
9 answers

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

Within a module, a controller can inherit properties from an outside controller: var app = angular.module('angularjs-starter', []); var ParentCtrl = function ($scope, $location) { }; app.controller('ChildCtrl', function($scope, $injector) { …
Federico Elles
  • 4,659
  • 7
  • 27
  • 35
164
votes
10 answers

AngularJS : The correct way of binding to a service properties

I’m looking for the best practice of how to bind to a service property in AngularJS. I have worked through multiple examples to understand how to bind to properties in a service that is created using AngularJS. Below I have two examples of how to…
112
votes
5 answers

Update parent scope variable in AngularJS

I have two controllers, one wrapped within another. Now I know the child scope inherits properties from the parent scope but is there a way to update the parent scope variable? So far I have not come across any obvious solutions. In my situation I…
97
votes
7 answers

How do I inject a controller into another controller in AngularJS

I'm new to Angular and trying to figure out how to do things... Using AngularJS, how can I inject a controller to be used within another controller? I have the following snippet: var app = angular.module("testApp",…
Scottie
  • 11,050
  • 19
  • 68
  • 109
81
votes
3 answers

Using $setValidity inside a Controller

I am trying to do some validation on file change. Here is my code: View/Template
Churk
  • 4,556
  • 5
  • 22
  • 37
70
votes
3 answers

Check if value exists in the array (AngularJS)

Currently, I'm using the forEach()-method of angular to check the new value with the array of objects. But that's the wrong approach because, for example, in the list are 20 objects. When I'm creating an object with an existing article, then the…
yuro
  • 2,189
  • 6
  • 40
  • 76
65
votes
20 answers

Error: [ng:areq] from angular controller

This is a long shot, but has anyone seen this error before? I am trying to add 'Transporters' using express, angular and mongoDB. I get this error whenever I access a page ruled by the transporters controller: Error: [ng:areq]…
Connor Leech
  • 18,052
  • 30
  • 105
  • 150
44
votes
3 answers

Accessing inherited scope with Controller As approach

With the original way to define controllers, accessing the parent's scope was fairly trivial, since the child scope prototypically inherits from its parent. app.controller("parentCtrl", function($scope){ $scope.name =…
New Dev
  • 48,427
  • 12
  • 87
  • 129
34
votes
4 answers

AngularJS passing data to bootstrap modal

I think I'm missing something but cannot figure what. Basically I'm trying to pass an object to the modal like below, but instead of getting the passed object I gets null...so I think is a problem with the scope but I'm new in Angular and need some…
31
votes
4 answers

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

I have this: app.controller('foo1', function ($scope) { $scope.bar = 'foo'; }); app.controller('foo2', function ($scope) { // want to access the $scope of foo1 here, to access bar }); How would I accomplish this?
Lucas
  • 16,930
  • 31
  • 110
  • 182
29
votes
3 answers

AngularJS: open a new browser window, yet still retain scope and controller, and services

I'm writing an angularJS app. In this particular controller, I open a new browser window through the $window.open service. But in the new window, all the $scope variables are lost. I tried to use window.parent but this doesn't work. In fact in the…
27
votes
5 answers

Calling directive's methods from parent controller in AngularJS

I am using AngularJS with the alias controllers pattern. I can't access (or I don't know how to) directive methods from a parent controller. I have a function inside my controller that should call a directive method but this directive method is not…
26
votes
4 answers

How to reuse one controller for 2 different views?

I have defined one controller, and apply it to 2 views with small differences. Angular code: app.controller('MyCtrl', function($scope) { $scope.canSave = false; $scope.demo = { files : [{ filename: 'aaa.html', source:…
Freewind
  • 193,756
  • 157
  • 432
  • 708
1
2 3
82 83