Angularjs: ‘controller as syntax’ and $watch

Just bind the relevant context. $scope.$watch(angular.bind(this, function () { return this.name; }), function (newVal) { console.log(‘Name changed to ‘ + newVal); }); Example: http://jsbin.com/yinadoce/1/edit UPDATE: Bogdan Gersak’s answer is actually kind of equivalent, both answers try binding this with the right context. However, I found his answer cleaner. Having that said, first and foremost, you … Read more

AngularJS – convert dates in controller

item.date = $filter(‘date’)(item.date, “dd/MM/yyyy”); // for conversion to string http://docs.angularjs.org/api/ng.filter:date But if you are using HTML5 type=”date” then the ISO format yyyy-MM-dd MUST be used. item.dateAsString = $filter(‘date’)(item.date, “yyyy-MM-dd”); // for type=”date” binding <input type=”date” ng-model=”item.dateAsString” value=”{{ item.dateAsString }}” pattern=”dd/MM/YYYY”/> http://www.w3.org/TR/html-markup/input.date.html NOTE: use of pattern=”” with type=”date” looks non-standard, but it appears to work in … Read more

Easiest way to pass an AngularJS scope variable from directive to controller?

Edited on 2014/8/25: Here was where I forked it. Thanks @anvarik. Here is the JSFiddle. I forgot where I forked this. But this is a good example showing you the difference between = and @ <div ng-controller=”MyCtrl”> <h2>Parent Scope</h2> <input ng-model=”foo”> <i>// Update to see how parent scope interacts with component scope</i> <br><br> <!– attribute-foo … Read more

How to use a filter in a controller?

Inject $filter to your controller function myCtrl($scope, $filter) { } Then wherever you want to use that filter, just use it like this: $filter(‘filtername’); If you want to pass arguments to that filter, do it using separate parentheses: function myCtrl($scope, $filter) { $filter(‘filtername’)(arg1,arg2); } Where arg1 is the array you want to filter on and … Read more