Questions tagged [angularjs-scope]

In AngularJS, a scope is an object that refers to the application model. It is an execution context for expressions.

In AngularJS, a scope is an object that refers to the application model. It is an execution context for expressions. Scopes are arranged in hierarchical structure which mimic the DOM structure of the application. Scopes can watch expressions and propagate events.

All scopes are children of the top level $rootScope using prototypical inheritance. A new child scope is created every time a directive in the DOM is configured to do so.

Common pitfall: using scalar variables on the scope within directives that create a new child scope (such as ng-if, ng-switch, ng-repeat, ng-include, ...) and not being able to reference them, due to the prototypal nature of JavaScript (own copy of variable is created in the child scope).

Useful links

8949 questions
95
votes
4 answers

Call a controller function from a directive without isolated scope in AngularJS

I cannot seem to find a way to call a function on the parent scope from within a directive without using isolated scope. I know that if I use isolated scope I can just use "&" in the isolated to access the function on the parent scope, but using…
Jim Cooper
  • 5,113
  • 5
  • 30
  • 35
95
votes
2 answers

Directive isolate scope with ng-repeat scope in AngularJS

I have a directive with an isolate-scope (so that I can reuse the directive in other places), and when I use this directive with an ng-repeat, it fails to work. I have read all the documentation and Stack Overflow answers on this topic and…
94
votes
5 answers

How to clear or stop timeInterval in angularjs?

I am making a demo in which I am fetching data from the server after regular intervals of time using $interval Now I need to stop/cancel this. How I can achieve this? If I need to restart the process, how should I do that? Secondly, I have one more…
user944513
  • 12,247
  • 49
  • 168
  • 318
94
votes
6 answers

Why is using if(!$scope.$$phase) $scope.$apply() an anti-pattern?

Sometimes I need to use $scope.$apply in my code and sometimes it throws a "digest already in progress" error. So I started to find a way around this and found this question: AngularJS : Prevent error $digest already in progress when calling…
Dominik Goltermann
  • 4,276
  • 2
  • 26
  • 32
92
votes
2 answers

AngularJS Directive element method binding - TypeError: Cannot use 'in' operator to search for 'functionName' in 1

This is the controller of the main template: app.controller('OverviewCtrl', ['$scope', '$location', '$routeParams', 'websiteService', 'helperService', function($scope, $location, $routeParams, websiteService, helperService) { ... …
CodeVirtuoso
  • 6,318
  • 12
  • 46
  • 62
86
votes
17 answers

Confirmation dialog on ng-click - AngularJS

I am trying to setup a confirmation dialog on an ng-click using a custom angularjs directive: app.directive('ngConfirmClick', [ function(){ return { priority: 1, terminal: true, link: function (scope,…
poiuytrez
  • 21,330
  • 35
  • 113
  • 172
84
votes
5 answers

AngularJS - How can I create a new, isolated scope programmatically?

I want to create an AlertFactory with Angular.factory. I defined an html template like follow var template = "

{{title}}

"; Title is provided by calling controller and applied as follow var compiled =…
Premier
  • 4,160
  • 6
  • 44
  • 58
80
votes
5 answers

Scope issues with Angular UI modal

I'm having trouble understanding/using the scopes for an angular UI modal. While not immediately apparent here, I have the modules and everything set up correctly (as far as I can tell), but these code samples in particular are where I'm finding…
coblr
  • 3,008
  • 3
  • 23
  • 31
79
votes
5 answers

Angular Directive refresh on parameter change

I have an angular directive which is initialized like so: I'd like it to be smart enough to refresh the directive when $scope.some_prop changes, as that implies…
Leah Sapan
  • 3,621
  • 7
  • 33
  • 57
78
votes
7 answers

AngularJS 1.5+ Components do not support Watchers, what is the work around?

I've been upgrading my custom directives to the new component architecture. I've read that components do not support watchers. Is this correct? If so how do you detect changes on an object? For a basic example I have custom component myBox which has…
78
votes
3 answers

How do I stop $watching in AngularJS?

I can set up a $watch on an AngularJS scope to be notified when the expression I am interested in has changed. But how do I stop watching once I lose interest?
Thilo
  • 257,207
  • 101
  • 511
  • 656
71
votes
5 answers

How do I share $scope data between states in angularjs ui-router?

Without using a service or constructing watchers in the parent controller, how would one give children states access to the main controller's $scope. .state("main", { controller:'mainController', url:"/main", templateUrl:…
sjt003
  • 2,407
  • 5
  • 24
  • 39
71
votes
9 answers

AngularJS directive does not update on scope variable changes

I've tried to write a small directive, to wrap its contents with another template file. This code: My cool content Should have this output:
My cool content
Because the layout…
Armin
  • 15,582
  • 10
  • 47
  • 64
70
votes
11 answers

Simple ng-include not working

Im playing with AngularJS for the first time, and im struggling to use ng-include for my headers and footer. Here's my tree: myApp assets - CSS - js - controllers - vendor - angular.js - route.js …
Oam Psy
  • 8,555
  • 32
  • 93
  • 157
68
votes
6 answers

How to call a function from another controller in AngularJS?

I need to call a function in another controller in AngularJS. How can I do this? Code: app.controller('One', ['$scope', function($scope) { $scope.parentmethod = function() { // task } } ]); app.controller('two',…
Bhavesh Chauhan
  • 1,023
  • 1
  • 11
  • 30