将控制器添加到ngRoute时出现意外的标识符

时间:2016-08-26 08:16:07

标签: javascript html angularjs

我遇到过这样的问题,当我将控制器添加到我的路由代码时,它无法识别出意外的标识符。不知道为什么会这样 这是我的routeProvider:

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

这是我的控制者:

    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');
      });
       }
    });

我在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>

3 个答案:

答案 0 :(得分:2)

您的代码中存在一些拼写错误:

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

不确定它是否能解决您的问题

答案 1 :(得分:1)

试一试。

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

答案 2 :(得分:1)

控制器名称必须作为字符串传递..这个

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