Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Unexpected identifier while adding controller to ngRoute

I've faced with such problem, when I add controller to my route code it fails to unexpected identifier. Have no idea why it's happening This is my routeProvider:

    app.config(function($routeProvider) {
        $routeProvider
          .when("/login", {
            title: 'Login',
            templateUrl: 'assets/login.html'
            controller: authCtrl
        })
    });

And this is my controller:

    app.controller('authCtrl', function ($scope, $rootScope, $routeParams, $location, $http, Data) {
    //initially set those objects to null to avoid undefined error
        $scope.login = {};
        $scope.signup = {};
        $scope.doLogin = function (customer) {
        Data.post('login', {
           customer: customer
         }).then(function (results) {
            Data.toast(results);
         if (results.status == "success") {
             $location.path('dashboard');
         }
         });
       };
        $scope.signup = {email:'',password:'',name:'',phone:'',address:''};
        $scope.signUp = function (customer) {
         Data.post('signUp', {
            customer: customer
         }).then(function (results) {
         Data.toast(results);
         if (results.status == "success") {
            $location.path('dashboard');
          }
       });
      };
      $scope.logout = function () {
        Data.get('logout').then(function (results) {
        Data.toast(results);
        $location.path('login');
      });
       }
    });

I've included such paths in my html:

   <script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.4.8/angular.min.js"/>
   <script src="app/angular-route.min.js"></script>
   <script src="app/angular-animate.min.js" ></script>
   <script src="app/toaster.js"></script>
   <script src="app/app.js"></script>
like image 915
Oleg V Avatar asked Aug 26 '16 08:08

Oleg V


3 Answers

There is some typos in your code :

 app.config(function($routeProvider) {
        $routeProvider
          .when("/login", {
            title: 'Login',
            templateUrl: 'assets/login.html', // <---- missing ','
            controller: 'authCtrl' // <----- should be format to string
        })
    });

Not sure if it will solve your problem

like image 55
Emidomenge Avatar answered Nov 15 '22 00:11

Emidomenge


try it.

app.config(function($routeProvider) {
        $routeProvider
          .when("/login", {
            title: 'Login',
            templateUrl: 'assets/login.html', 
            controller: 'authCtrl'   // <-- string
        })
    });
like image 30
Qiu Avatar answered Nov 15 '22 00:11

Qiu


the controller name must pass as a string..try this

app.config(function($routeProvider) {
    $routeProvider
      .when("/login", {
        title: 'Login',
        templateUrl: 'assets/login.html', 
        controller: 'authCtrl'
    })
});
like image 20
Tarek Salah uddin Mahmud Avatar answered Nov 15 '22 00:11

Tarek Salah uddin Mahmud