Haddar Macdasi
Haddar Macdasi

Reputation: 3555

Why controller as is undefined?

I've forked some code on Plunker in AngularJS and am trying to rewrite it with John Papa's conventions in mind.

I get an error:

Argument 'testCtrl as ctrl' is not a function, got undefined

Why ?

(function() {

    angular.module('Test', [])
        .controller('testCtrl', testCtrl)
        .directive('testDirective', testDirective);


    function testCtrl() {
        var vm = this;

        vm.options = {};

        vm.callMe = function() {
            console.log("called from crontroller");
            vm.options.method1("something");
        };

    }

    function testDirective() {
        return {
            restrict: "E",
            scope: {
                options: '='
            },
            link: function(scope, element, attrs) {

                scope.options.method1 = function(message) {
                    console.log("called from crontroller: " + message);
                }
            }
        }
    }
}());

Upvotes: 2

Views: 165

Answers (1)

Kevin B
Kevin B

Reputation: 95048

The version of Angular used in your plunkr doesn't support ControllerAs, update it to a newer version.

Upvotes: 1

Related Questions