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
25
votes
1 answer

Karma testing: TypeError: Attempted to assign to readonly property

When I am trying to unit test my controller I am getting the error.When I debug the testcase's expect I am getting the expected value but its failing with below error.What am I missing here or whether I am testing the controller variable in a wrong…
Raphael
  • 1,738
  • 2
  • 27
  • 47
23
votes
2 answers

Angular.js best practice - extending controllers, overriding controller defaults

Here is a real-life Angular problem I can't wrap my head around. I love Angular, but this issue is bugging me a lot right now. What is the best practice to extend an existing controllers's functions, and use the extended controller on an other page…
Mihaly KR
  • 2,363
  • 2
  • 19
  • 20
21
votes
4 answers

AngularJS: Inject controller inside another controller from the same module

Is possible to inject a controller into another controller that is part of the same module? example: var app = angular.module('myAppModule', []) .controller('controllerOne', ['$scope', function($scope){ $scope.helloWorld = function(){ …
21
votes
8 answers

AngularJS ng-controller not working

I just now started learning on AngularJS from w3schools. I am trying to practice examples what ever they have mentioned in the tutorials. Every thing works fine but when i came to "AngularJS Controllers" it is not working properly as working well in…
UiUx
  • 967
  • 2
  • 14
  • 25
21
votes
2 answers

How am I supposed to pass data from serverside controller an AngularJS controller?

I've encountered what I believe to be a common scenario, in which I am using an MVC pattern (specifically ASP.NET's MVC framework) for a web application, with AngularJS on the front-end. My problem is that I have a particular value which is part of…
20
votes
3 answers

Angular-ui bootstrap modal without creating new controller

plunk: http://plnkr.co/edit/85Wl5W If I use the $modalInstance on the same controller(modalController.js), without being in a modal, angular gets frozen. I just want to simplify my life using angularjs with the angular-ui bootstrap modal service. I…
20
votes
3 answers

How to access parent directive's controller by requiring it recursively?

I'm trying to reach the controller of a parent "box" directive recursively:
Rhs
  • 3,188
  • 12
  • 46
  • 84
19
votes
5 answers

AngularJS controllers, design pattern for a DRY code

I have created a full example for the purpose of describing this issue. My actual application is even bigger than the presented demo and there are more services and directives operated by every controller. This leads to even more code repetition. I…
Atais
  • 10,857
  • 6
  • 71
  • 111
19
votes
2 answers

How can we test non-scope angular controller methods?

We have few methods in Angular Controller, which are not on the scope variable. Does anyone know, how we can execute or call those methods inside Jasmine tests? Here is the main code. var testController = TestModule.controller('testController',…
18
votes
2 answers

"controller as" vs isolate scope

I'd like to use the 'controllerAs' option in my directives. Let me cite the reasoning from "An AngularJS Style Guide for Closure Users at Google": Why? Putting methods and properties directly onto the controller, instead of building up a scope…
16
votes
3 answers

Call controller function from service in angularjs

I am using socket.io to enable chat in my app and i am using a service SocketService to perform all the socket stuff. When a message came then i want to trigger a function of a controller from the service SocketService to make some changes in the…
16
votes
5 answers

AngularJS: Should I convert directive's linking function to a controller?

I heard it's a good practice to use the controllerAs syntax along with bindToController: true in directives that use an isolate scope. References: one, two Suppose, I have a directive like this: angular.module('MyModule').directive('MyDirective',…
Misha Moroshko
  • 166,356
  • 226
  • 505
  • 746
16
votes
2 answers

specifying AngularJS controller: benefits using ngController vs. $routeProvider

There are two ways (AFAIK) to associate a controller with a view template/partial: the route specified in $routeProvider and the ngController directive. Especially (but not exclusively) for simple routing, is there any benefit/efficiency of one over…
nshew13
  • 3,052
  • 5
  • 25
  • 39
1
2
3
82 83