angularjs - Why can't I pass $scope and $router in controller params? -
(function() { function appcontroller($router) { $router.config([ {path: '/courses',component: 'courses'}, {path: '/',component: 'welcome'} ]); } angular.module('mainapp', ['ngnewrouter']) .controller('maincontroller', ['$router', 'courserepository', appcontroller, '$scope', function($scope) { $scope.$on('clicked', function(e){ $scope.tgstate = !$scope.tgstate; }) }]); })();
i trying connect new angular route in 1 controller function wrote. works if in 2 separate controllers, somehow doesn't when i'm trying connect in 1 controller.
apart confusing use of same name (are trying merge 2 functions? mean "connecting them"? can't in js, see below), you're instantiating wrong. this:
.controller('appcontroller', ['$router', 'courserepository', appcontroller, '$scope', function($scope) {
should this:
.controller('appcontroller', ['$router', 'courserepository', 'appcontroller', '$scope', function($router, courserepository, appcontroller, $scope) {
if controller function doesn't have same number of arguments controller array (minus function), it's dead giveaway wrong. also, in format, appcontroller
needs declared service or factory or provider...
more info controllers: https://docs.angularjs.org/guide/controller
however, if you're trying merge 2 functions, won't work, see simple example:
function f() { alert('a'); } function f() { alert('b'); } // no, won't alert "ab" f();
Comments
Post a Comment