nofear87
nofear87

Reputation: 869

How to call Controller?

var modalInstance = $modal.open({
    templateUrl: 'views/modals/addDevice.html',
    controller: ModalCtrl
});

modalInstance.result.then(function (newDevice) {

});

I will implement it in this way:

app.controller("ModalCtrl", function( $scope, $modalInstance ) {

});

But only this way is accepted:

var ModalCtrl = function ($scope, $modalInstance) {

};

Why this happens?

Upvotes: 0

Views: 98

Answers (4)

Shivaraj
Shivaraj

Reputation: 613

Use cotes for the controller name. That will work fine.

Upvotes: 0

Ross
Ross

Reputation: 3330

I think you are having issues because the value for your controller name needs to be in quotes -

var modalInstance = $modal.open({
    templateUrl: 'views/modals/addDevice.html',
    controller: 'ModalCtrl' // <-- this needs to be in quotes
});

Upvotes: 2

Petro
Petro

Reputation: 3662

This is defining your controller class:

 var ModalCtrl = function ($scope, $modalInstance) {

 };

you need to create an instance of the class:

  var instance = new ModalCtrl(args)

Upvotes: 0

tasseKATT
tasseKATT

Reputation: 38490

When registering a controller the following way:

app.controller("ModalCtrl", function( $scope, $modalInstance ) {

});

You haven't declared a ModalCtrl variable so in the following case ModalCtrl would be undefined:

var modalInstance = $modal.open({
    templateUrl: 'views/modals/addDevice.html',
    controller: ModalCtrl
});

You can provide the controller name as a string instead:

var modalInstance = $modal.open({
    templateUrl: 'views/modals/addDevice.html',
    controller: 'ModalCtrl'
});

Upvotes: 3

Related Questions