Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cannot bind to controller without identifier for directive + angularjs

I have a state router as such:

$stateProvider
  .state('home', {
    url: '/',
    templateUrl: 'spa/layouts/home.html',
    controller: 'HomeController',
    controllerAs: 'ctrl'
  });

In my home.html template I have:

<div class="row main-body">
  <aside class="col-md-2 sidebarNav">
    <div>...</div>
  </aside>

  <section class="col-md-10 main-container">
    <div>..</div>

    <my-list list-items={{ ctrl.listItems }}></my-list>
  </section>
</div>

In the directive my-list I have the following:

var templateUrl = 'spa/components/classList/classList.html';

angular
  .module('directives')
  .directive('myList', component);

function component() {
  return {
    templateUrl: templateUrl,
    controller: classListDir,
    contollerAs: '$ctrl',
    scope: {
      listItems: '@'
    },
    bindToController: true,
  };
}

classListDir.$inject = ['$scope'];

function classListDir($scope) {
  var $ctrl = this;
  console.log($ctrl);
}

I have read and re-read https://docs.angularjs.org/error/$compile/noident?p0=myList. I think my case deals with the second

//OKAY, because the directive uses the controllerAs property to override

// the controller identifier.

I still keep getting the error message. I don't understand the binding with identifier error. Can someone please explain this.

like image 947
runningincircles Avatar asked Apr 21 '16 14:04

runningincircles


People also ask

How do I bind to a controller in angular?

Binding to controllers with bindToController. Angular 1.3 introduces a new property to the directive definition object called bindToController, which does exactly what it says. When set to true in a directive with isolated scope that uses controllerAs, the component’s properties are bound to the controller rather than to the scope.

Why do we need to bind to a directive in angular?

That means, Angular makes sure that, when the controller is instantiated, the initial values of the isolated scope bindings are available on this, and future changes are also automatically available. Let’s apply bindToController to our directive and see how the code becomes cleaner.

How to use ng-controller directive in AngularJS?

The ng-controller Directive in AngularJS is used to add controller to the application. It can be used to add methods, functions and variables that can be called on some event like click, etc to perform certain action. Where expression refers to the name of the controller.

What is difference between link function and controller in AngularJS directives?

This post is a part of AngularJS Directives Tutorial Series. In previous posts, we have discussed link function. link function are specific to directive instance and can be used to define directive’s behavior & business logic. Controller in directive's on the other hand are used for Directive’s inter-communication.


1 Answers

Found the issue:

contollerAs: '$ctrl'

was misspelled.

like image 80
runningincircles Avatar answered Nov 14 '22 22:11

runningincircles