3

I am trying to do Controller As Syntax in Angular. At this point, I have it in my routerProvider...not sure if it would matter for the issue I am having, but here it is anyways:

 angular.module('ucp.kick', [
  'ngRoute'
])
.config ($routeProvider, APP_BASE_URL) ->
  $routeProvider
  .when APP_BASE_URL + 'kicks',
    name: 'Kicks'
    templateUrl: 'kick/partials/kick.html'
    controller: 'kick as KickController'

Here is a condensed version of my controller I have:

  this.$watchCollection('filtered.devices', function(devices) {
    return this.filteredDevices = devices;
  });

But I get:

TypeError: Object [object Object] has no method '$watchCollection'

I realize when using the controller as syntax, you do not want inject the scope. So, how do I access $watchCollection function?

3 Answers 3

5

You will still need to inject $scope in order to use $watch and $watchCollection. Now, you would think you could do:

$scope.$watchCollection('filtered.devices', function (newVal, oldVal) {});

or

$scope.$watchCollection('this.filtered.devices', function (newVal, oldVal) {});

But that won't work. So you need to either do:

var that = this;
$scope.$watchCollection(function () {
    return that.filtered.devices;
}, function (newVal, oldVal) {

});

or:

$scope.$watchCollection(angular.bind(this, function () {
    return this.filtered.devices;
}), function (newVal, oldVal) {

});

or:

$scope.$watchCollection("KickController.filtered.devices", function(newVal, oldVal) { });
Sign up to request clarification or add additional context in comments.

1 Comment

Fascinating! Never new about that
1

controller as does not replace the controller with $scope. Controllers don't have a $watchCollection property. You still have to inject $scope and use $scope.$watchCollection and you have to use the controller identifier as the name of the collection to watch

$scope.$watchCollection("KickController.filtered.devices"

That being said, using $watch and $watchCollection can usually be avoided and you can just work with the data bindings. Angular will use $watchCollection internally on the collection. Specifically I'm not sure that you need this other property filteredDevices when you can just use a filter explicitly on the existing collection.

Comments

1

For watches, you still need to use $scope.

After injecting $scope into your controller, as you would have before the controllerAs syntax came about, you could do: $scope.$watchCollection(..)

To further iterate on what I mean: if the value you are trying to watch is bound to a value using the controllerAs syntax -- such as kickController.someValue then you would watch that value $scope.$watchCollection('kickController.someValue',...)

Sidenote: showing all of your code would help because it looks like you could have your controller definition backwards. Did you mean to do: ... controller: KickController as kick

and then:

var kick = this;

?

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.