ng-click不会调用离子函数?

时间:2016-12-03 09:06:01

标签: angularjs cordova ionic-framework

以下模板代码中的ng-click不会调用控制器中的函数loginFacebook()?文本'loginFacebook ...'不会在Javascript控制台中打印。

slogin.html

<ion-view title="Login" id="page100">
    <ion-content>
        <a class="button" ng-click="loginFacebook()">
            Sign In With Facebook
        </a>
    </ion-content>
</ion-view>

控制器:

.controller('LoginCtrl', ['client', function ($scope, $state, client) {
    $scope.loginFacebook = function () {
        console.log('loginFacebook...')
        client.login("facebook").then(function succes(data){
            console.log('logged in succesfully..')
            $state.go('menu.events');
        }, function(error){
            console.log('login failed.');
        });
    };
}])

routes.js

  .state('menu.login', {
    url: '/login',
    views: {
        'side-menu21': {
            templateUrl: 'templates/slogin.html',
            controller: 'LoginCtrl'
        }
    }
  })

1 个答案:

答案 0 :(得分:2)

您按照注射顺序缺少 $scope $state

你的控制器应该是,

.controller('LoginCtrl', ['$scope','$state','client', function ($scope, $state, client) {
        $scope.loginFacebook = function () {
            console.log('loginFacebook...')
            client.login("facebook").then(function succes(data){
                console.log('logged in succesfully..')
                $state.go('menu.events');
            }, function(error){
                console.log('login failed.');
            });
        };
    }])