How to start the controller?

I had a problem trying to use a different syntax to start the controller in a script tag.

Why does it work:

function ListCtrl($scope, Projects) { $scope.projects = Projects; } 

and this is not:

 myProject.controller('ListCtrl', ['$scope', 'Projects', function ($scope, Projects) { $scope.projects = Projects; }]); 

Here is the full plunker http://plnkr.co/edit/Po16QUxmu3M3FqIGqJ3Y?p=preview

Thanks in advance, - Jan

+4
source share
1 answer

When using controller syntax , you also need to change all routes that used a function reference to use a string reference

Interesting : using a string reference will also work when defining global controller functions, but for now, it is best to use .controller syntax and avoid global functions .. p>

 var myProject = angular.module('project', ['firebase']). value('fbURL', 'https://angularjs-projects.firebaseio.com/'). factory('Projects', function(angularFireCollection, fbURL) { return angularFireCollection(fbURL); }). config(function($routeProvider) { $routeProvider. when('/', {controller:'ListCtrl', templateUrl:'list.html'}). otherwise({redirectTo:'/'}); }); // function ListCtrl($scope, Projects) { // $scope.projects = Projects; // } // next 3 lines will work myProject.controller('ListCtrl', ['$scope', 'Projects', function ($scope, Projects) { $scope.projects = Projects; }]); 
+3
source

Source: https://habr.com/ru/post/1489794/


All Articles