How do I ignore the initial load when watching model changes in AngularJS?

I have a web page that serves as the editor for a single entity, which sits as a deep graph in the $scope.fieldcontainer property. After I get a response from my REST API (via $resource), I add a watch to ‘fieldcontainer’. I am using this watch to detect if the page/entity is “dirty”. Right now … Read more

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 scope }); angular.module(‘myApp’).controller(‘myCtrl2’, function($scope) { var b = //get var a from root scope somehow //use var b }); … Read more

When writing a directive in AngularJS, how do I decide if I need no new scope, a new child scope, or a new isolated scope?

I’m looking for some guidelines that one can use to help determine which type of scope to use when writing a new directive. Ideally, I’d like something similar to a flowchart that walks me through a bunch of questions and out pops the correct answer – no new new scope, new child scope, or new … Read more

$watch an object

I want to watch for changes in a dictionary, but for some reason watch callback is not called. Here is a controller that I use: function MyController($scope) { $scope.form = { name: ‘my name’, surname: ‘surname’ } $scope.$watch(‘form’, function(newVal, oldVal){ console.log(‘changed’); }); } Here is fiddle. I expect $watch callback to be fired each time … Read more

$rootScope.$broadcast vs. $scope.$emit

Now that the performance difference between $broadcast and $emit has been eliminated, is there any reason to prefer $scope.$emit to $rootScope.$broadcast? They are different, yes. $emit is restricted to the scope hierarchy (upwards) – this may be good, if it fits your design, but it seems to me a rather arbitrary restriction. $rootScope.$broadcast works across … Read more

AngularJS access parent scope from child controller

I’ve set up my controllers using data-ng-controller=”xyzController as vm” I have a scenario with parent / child nested controllers. I have no problem accessing parent properties in the nested html by using $parent.vm.property, but I cannot figure out how to access the parent property from within my child controller. I’ve tried injecting $scope and then … Read more

AngularJS : Prevent error $digest already in progress when calling $scope.$apply()

I’m finding that I need to update my page to my scope manually more and more since building an application in angular. The only way I know of to do this is to call $apply() from the scope of my controllers and directives. The problem with this is that it keeps throwing an error to … Read more

‘this’ vs $scope in AngularJS controllers

In the “Create Components” section of AngularJS’s homepage, there is this example: controller: function($scope, $element) { var panes = $scope.panes = []; $scope.select = function(pane) { angular.forEach(panes, function(pane) { pane.selected = false; }); pane.selected = true; } this.addPane = function(pane) { if (panes.length == 0) $scope.select(pane); panes.push(pane); } } Notice how the select method is … Read more

What is the difference between ‘@’ and ‘=’ in directive scope in AngularJS?

I’ve read the AngularJS documentation on the topic carefully, and then fiddled around with a directive. Here’s the fiddle. And here are some relevant snippets: From the HTML: <pane bi-title=”title” title=”{{title}}”>{{text}}</pane> From the pane directive: scope: { biTitle: ‘=’, title: ‘@’, bar: ‘=’ }, There are several things I don’t get: Why do I have … Read more