0

I am getting the above error within a partial template loaded into the main html. When I declare the controller as global it works (MyController1), also when I declare the controller directly within "app.controllers" it works, but when I declare the controller (MyController) as part of the partial template the above error appears. Any help?

Code

<div ng-controller='MyController1'>
    <span ng-bind="mydesc"></span>
</div>

<script type="text/javascript">

function MyController1($scope) {
    $scope.mydesc = "Direct Global Definition";
    console.log('in Direct');
}

angular.module('app.controllers').controller('MyController',['$scope',function($scope) {
    $scope.mydesc = "Defined as part of Controller Inline";
}]);

</script>

The above code works, but when I change ng-controller="MyController1" to ng-controller="MyController", the error appears.

I don't want to use global functions and I can't add every partial controller to "app.controllers".

  • http://stackoverflow.com/questions/26646941/getting-an-error-when-using-ng-controller-in-angularjs-ver-1-3-0/26647015#26647015 – Kalhan.Toress Apr 08 '15 at 09:22

1 Answers1

0

Looks, like you use angular 1.3 version. In that version global functions as controllers were disabled according to release notes, but still, you're able to use them. All you need to do is to configure controller provider to allow global methods to be used as controllers. Here is full working code

<div  ng-app="app.controllers" ng-controller="MyController1">
    <span ng-bind="mydesc"></span>
</div>

Though it's not recommended to use global methods.

function MyController1($scope) {
  $scope.mydesc = "Direct Global Definition";
}
app = angular.module('app.controllers', []);
app.config(['$controllerProvider', function($controllerProvider) {
     $controllerProvider.allowGlobals();
}]);
app.controller('MyController', ['$scope', function ($scope) {
    $scope.mydesc = "Regular definition";
}]);
Oleg
  • 1,100
  • 2
  • 11
  • 16
  • Thanks Oleg: The question is not for the global function, that works, but it is the controlelr being defined as part of the app. When I define the controller as part of the app it does not work. When I define the controller within the "app.controllers".js file, it works, but not as part of the partial html file. – Gerrit van Dyk Apr 09 '15 at 09:52
  • So if I got it correctly, you want to store controllers inside html templates, so all your html files are self contained. Right? – Oleg Apr 09 '15 at 10:10
  • Yes Oleg. You are 100% correct. I am also getting similar errors when I want to add directives or anything else in "html files", so that they are self contained. – Gerrit van Dyk Apr 14 '15 at 08:00