简体   繁体   中英

Using A Controller in a Custom Angular Directive

I've been following along with a codeschool Angular tutorial and my app works fine until adding this custom directive:

// Breaks here 
app.directive('reviewForm', function() {
   return {
        restrict: 'E',
        templateUrl: 'partials/review-form.html',
        replace: true,
        controller: function() {
            this.showForm: false;
        },
        controllerAs: 'reviewFormCtrl',

    }
})

And here is the error I receive via console.log:

Uncaught Error: [$injector:modulerr] Failed to instantiate module myApp due to:
Error: [$injector:nomod] Module 'myApp' is not available! You either misspelled 
the module name or forgot to load it. If registering a module
ensure that you specify the dependencies as the second argument.

I think it is spelling mistake, your module name is myApp, and in your code you wrote:

app.directive('reviewForm', function() {......

it should be myApp.directive , like this:

myApp.directive('reviewForm', function() {
   return {
        restrict: 'E',
        templateUrl: 'partials/review-form.html',
        replace: true,
        controller: function() {
            this.showForm: false;
        },
        controllerAs: 'reviewFormCtrl',

    }
})

if it did not work, check the spelling of your module name.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM