The angularjs controler 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.
76
votes
4answers
42k views
How can I pass variables between controllers in AngularJS?
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 ...
45
votes
3answers
16k views
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 ...
38
votes
2answers
10k views
Can an AngularJS controller inherit from another contoller 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) {
};
...
21
votes
3answers
25k views
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 = ...
7
votes
1answer
3k views
Getting AngularJS Error: “[$rootScope:inprog] $digest already in progress” without a manual $apply
Other posts on this error always include someone trying to $apply without using a safe apply, but that's not the case in my example. My function IS successfully returning the data I requested from the ...
6
votes
4answers
2k views
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 : ...
6
votes
4answers
1k views
Angular ngController vs Controller constructed within Directive
I'm wondering what the use cases are for these two methods of creating a controller:
Using ngController:
myApp.controller('myController', ['$scope', function ( $scope ) {
}]);
Constructing the ...
6
votes
2answers
4k views
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 ...
6
votes
1answer
3k views
Sharing data between AngularJS controllers? [duplicate]
How do I store the items I've selected in a checkbox with other controllers?
My attempt (see the plnkr for views):
script.js (controllers)
var myApp = angular.module('myApp', []);
...
5
votes
2answers
1k views
Using AngularJS and jsPlumb (use jsPlumb functions in AngularJS controller)
So I have a project that I am working on and it requires that I use jsPlumb for graphical elements connections and I am building my app entirely using AngularJS.
What is the procedure to follow if I ...
5
votes
2answers
796 views
angularjs: preventing route changes
I've tried doing the following in my controller:
$scope.$on("$routeChangeStart", function (event, next, current) {
if (!confirm('are you sure ?')) {
event.preventDefault(); ...
4
votes
3answers
8k views
Simple Angular $routeProvider resolve test. What is wrong with this code?
I have created a simple Angular JS $routeProvider resolve test application. It gives the following error:
Error: Unknown provider: dataProvider <- data
I would appreciate it if someone could ...
4
votes
1answer
2k views
AngularJS - How do I avoid using $timeout to wait for an element to be created?
Here is the idea:
So I am trying to use an external library function to create some manipulation in the DOM in one of my controllers that my ng-repeat is connected to.
Newest jsFiddle working but ...
3
votes
1answer
3k views
AngularJS The scope for dynamic content through $compile isn't attached to the controller scope
When I generate a new element through a string that has a directive (that's why I need to compile) and that directive generates an association with a variable in the controller scope through "=", the ...
3
votes
2answers
51 views
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 ...
3
votes
1answer
145 views
Jasmine Test in Angular for controller
I get the following error: TypeError: undefined is not a function
The problem is that the common is module and a factory and the problem is on my line
var ctrl = ...
3
votes
1answer
2k views
AngularJS Binding, multiple controllers through a service, part of page rendered from php
I know this is long, but it appears something specific in what I'm doing in the more complex scenario is causing the issue. All simple examples I attempt work fine.
I have an application setup using ...
3
votes
2answers
2k views
What's the correct way to trigger jQuery DOM Manipulation from within a controller?
So I keep reading that jQuery manipulation from within a Controller is bad practice, but I'm not clear on why, or how to correct.
Below is code from a Youtube tutorial which even the video creator ...
3
votes
1answer
93 views
How do I resolve and assign an inner controller from the outer's scope?
Self explanatory fiddle: http://jsfiddle.net/5FG2n/1/
Say I have a view with two controllers, one containing the other. The outer controller is static, but I need to set the inner controller based ...
3
votes
2answers
757 views
AngularJS common controller functionality - mix-in where needed or define on $rootScope?
I am using v1.2.0 rc2 of AngularJS and want to know what is the best method to provide common functionality to multiple controllers.
I have the following validation functions that I want to use in ...
3
votes
1answer
473 views
Nested directives - cannot pass args to controller method from child directive in Angularjs
I'm having some trouble with nested directives in angularjs. I want to call a controller method from a directive within another directive and am trying to pass arguments to it, however they are ...
3
votes
0answers
49 views
The difference between a compile/link function and a controller function in AngularJS? [duplicate]
I'm trying to write my own AngularJS directives. I think I understand the difference between the compile and link functions. This video clears it up nicely.
But I'm not sure now that I understand the ...
3
votes
2answers
1k views
AngularJS: Sending requests via $resource with array type parameters (checkboxes, multiselects etc.)
I have problems trying to use the $resource library in AngularJS to send a properly serialized GET request when there is an array of checkboxes (client_status) in my GET parameters.
This is the code ...
2
votes
3answers
3k views
AngularJs - Error: 10 $digest() iterations reached. Aborting
I am trying to create a Metro Tile type grid with Angular, to achieve this i want each of the tiles to be a different colour. So my plan of action was to create a function that would randomly pick a ...
2
votes
3answers
3k views
Angular communication between controllers and directives
I have this piece of code which allows a user to leave comments on a list of items.
I created a directive and listen to keydown in order to let the user submit a comment if keyCode == 13.
Not sure ...
2
votes
1answer
2k views
Using a child controller from another module
I've defined a module and made it the main module for my app using the ng-app directive. I added two controllers to the main app using angular.module('myApp').controller(). One of those controllers ...
2
votes
2answers
3k views
AngularJS - Scope is not what expected inside an ng-click event of ng-repeat
I have this code snippet:
<ul>
<li ng-repeat="message in messages">
<button ng-click="send()">Send</button>
</li>
</ul>
$scope.send = function(){
...
2
votes
1answer
25 views
How to avoid code duplication when using controllers with similar methods?
Say I have following controllers:
controller("MyCtrl1", ["$scope", "$sce", "myService", "$location",
function ($scope, $sce, myService, $location) {
$scope.Resources = ...
2
votes
2answers
1k views
Load controller dynamically based on route group
Is it possible to load a controller, it's js file, and a template dynamically based on a route group? Psuedo code which doesn't work:
$routeProvider.when('/:plugin', function(plugin) {
templateUrl: ...
2
votes
1answer
62 views
What are the differences in angular controller notation?
I've have seen a couple of notations to initialize a controller in angular, those are:
app.controller('nameCtrl', function($scope, ... ){})
and
app.controller('nameCtrl', ...
2
votes
3answers
366 views
How do I access the $scope variable of one controller from another in Angular.js?
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 ...
2
votes
1answer
1k views
Pass Arguments from directive to controller function
I've seen lots of these questions but haven't found a solution that works. here is a fiddle that doesn't work but should.
http://jsfiddle.net/cdparmeter/j2K7N/2/
Controller:
$scope.foo = function ...
2
votes
1answer
67 views
How can we test non-scope angular controller methods?
We have few methods in Angular Controller, which are not on the scope variable.
Is anyone know, how can we execute or call those methods inside jasmine test?
Here is main code.
var testController ...
2
votes
2answers
132 views
Did I share state correctly outside angular directive? (parent scope / $rootScope issue)
I have two elements on my page which are not nested in some way. With attributes on the first one, I would like to set the content of the second one, like so:
<body>
<input info="This is ...
2
votes
2answers
1k views
Loading Modules in Angular
I'm new to AngularJS. In my efforts to learn, I've relied on the AngularJS tutorials. Now, I'm trying to build an app using the AngularSeed project template. I'm trying to make my project as modular ...
2
votes
1answer
2k views
Jquery animation in a AngularJS controller or directive?
I have an app in AngularJS and in one of my view I want to implement some JQuery animation..nothing too fancy..just hide/slide of a divs ..those divs are created from ng-reapet loop and I need access ...
2
votes
1answer
2k views
How to access the $ngModelController from inside the Controller without a Form and without a Directive
Maybe it's a rookie mistake, but I can't seem to access the $scope.model's $ngModelController so I can grab the $viewValue from it.
I have an input without a form (im using ui-mask directive):
...
2
votes
1answer
38 views
How do I assign an attribute to ng-controller in a directive's template in AngularJS?
I have a custom attribute directive (i.e., restrict: "A") and I want to pass two expressions (using {{...}}) into the directive as attributes. I want to pass these attributes into the directive's ...
2
votes
1answer
76 views
Angular binding model in scope issue?
Hi I am new to the angular js and trying to write the chat application using the socket io and angular js with ionic for android platform. But in my chat page there is one issue.
I am trying to bind ...
2
votes
1answer
61 views
Getting just an object back from an asynchronous call
My goal is
to show a username, which needs to manipulated on a web page. The mnaipulation should be done centrally in a service (MasterDataService). I want to used a scope variable on the screen which ...
2
votes
1answer
151 views
Angular Controller Instantiated Multiple Times
I have an Angular controller loaded in a view:
<div ng-controller="MyCtrl">
<p ng-bind-html="content"></p>
</div>
This partial is loaded into different views, and as a ...
2
votes
1answer
605 views
What's the right way to wait for ng-init values to be set?
I have an "ng-init" directive in my HTML that initializes the value of "data.id" in my Angular.js data model. Let's say for now that I can't change how this works.
Now, I want to make an HTTP request ...
2
votes
1answer
431 views
Length of Array returning “undefined”
I am building an application using AngularJS. I use Slim for handling web service in my application.
I have a service like:
angular.module('MyApp').
factory('MainPage', ...
2
votes
1answer
116 views
What problems would I run into if I make all Data available to all Controllers?
My AngularJS CRUD application processes it's information over a WebSocket Server. (This was mainly so that updates from one user would get automatically pushed to all users without the need for ...
2
votes
0answers
93 views
Angular directive in ng-repeat doesn't call link
I'm fairly new to Angular and am having some trouble getting a directive to render within an ng-repeat. This is probably a lack of experience on my part so any help is greatly appreciated.
Here are ...
2
votes
1answer
98 views
How to dynamically load controller to a directive
So I have a directive that Will be acting as a side panel in my app. When a user clicks a button the side panel will open. In said side panel I need the controller and view for this area to be ...
2
votes
1answer
53 views
Directives & Controllers: resolve, loading, reloading [closed]
I need a code for the next problem. I know which components ($http, $q, etc.) I have to use. I don't know how to do it in a right way! So,
I need an AngularJS directive or controller,
It loads some ...
2
votes
1answer
154 views
Controller function calls more than once in AngularJs
I am making mobile application with using JQuery Mobile and AngularJs
I am currently using single page structure here is my sample
<body ng-app="myApp">
<div data-role="page" ...
2
votes
1answer
2k views
Argument 'PricesController' is not a function, got undefined
I am relatively new to angularjs so please bare with me. I have looked at a lot of examples related to setting up the app, and everything I'm doing seems correct but I'm still getting the error ...
2
votes
2answers
81 views
AngularJS Directives and the Model
I'm not sure this question's been asked, but is it good practice (not ok-to-do) to mess with the data model from an AngularJS directive?
For instance, if in my controller I have some object, like:
...