无法从Meteor模板调用Angular Controller

时间:2014-12-12 21:07:01

标签: angularjs meteor router iron-router

我已将Meteor项目从路由器更新为Iron-router; 我使用Iron-router创建了客户端/服务器端路由。

更新到铁路由器后,当我点击提交按钮时,我在调用LoginController时遇到问题,它没有进入doLogin功能。我正在处理angular.doLogin函数中的所有功能来处理登录功能。

客户端路线:

Router.route('/', 'login');

Router.route('/login', function () {
    this.render('login');
});

登录模板:

<template name="login">
    <div class="login-box" ng-controller="LoginController">           
        <h6>
            <span>Welcome!</span>
            Please log in using your username and password.
        </h6>
        <form ng-submit="doLogin();
                        isClick = false" name="myform" >
            <fieldset>
                <input type="text" id="login-username" placeholder="Username" name="username" ng-model="username" autocomplete="off" ng-click="msg = ''">
                <input type="password" id="login-password" placeholder="Password" name="password" ng-model="password" autocomplete="off">
            </fieldset>
            <span>{{msg.message}}</span>
         <button id="but" type="submit" ng-disabled="!username || !password">log in</button>
        </form>
    </div>
</template>

登录控制器:

angular.module('RSC').controller('LoginController', function ($scope, $route, $routeParams, $location, $http, $window, $sce) {   
    $scope.loggedIn = $window.sessionStorage['loggedIn'] ? angular.fromJson($window.sessionStorage['loggedIn']) : {status: false};
    $scope.sessionid = Meteor.createSessionId();
    // Add the login method
    $scope.doLogin = function () {
        var username = $scope.username;
        var password = $scope.password;
        // Adding the login to help generate the clientId
        if (!$window.localStorage['clientid_' + username]) {
            $window.localStorage['clientid_' + username] = Meteor.RSCRandomNumber();
        }
        // Get client id from local storage
        var clientid = $window.localStorage['clientid_' + username];
        // build the parameters
        var dataSent = {"username": username, "password": password, "clientid": clientid};
        return $http({
            url: '/req/login',
            method: 'POST',
            data: jQuery.param(dataSent),
            headers: {'Content-Type': 'application/x-www-form-urlencoded'}
        }).error(function (err) {
            $scope.msg = err;

        }).success(function (result) {
            //resolve the promise as the data
            if (!result.hasOwnProperty('info')) {
                $scope.username = "";
                $scope.password = "";
                $scope.msg = result.error;
                $window.sessionStorage['userInfo'] = JSON.stringify({});
                $window.sessionStorage['loggedIn'] = JSON.stringify({status: false});
            } else {
                $window.sessionStorage['userInfo'] = JSON.stringify(result.info);
                $window.sessionStorage['loggedIn'] = JSON.stringify({status: true});
                $scope.msg = "";
                Session.set("vendorId", result.info.vendorId);
                $location.path("/home");
            }
        });
    };
});

1 个答案:

答案 0 :(得分:0)

我能够解决问题,以下解决方案帮助了我:https://github.com/Urigo/angular-meteor/issues/48

if (Meteor.isClient) {
    Router.onAfterAction(function (req, res, next) {
        Tracker.afterFlush(function () {
            angular.element(document).injector().invoke(['$compile', '$document', '$rootScope',
                function ($compile, $document, $rootScope) {
                    $compile($document)($rootScope);
                    if (!$rootScope.$$phase)
                        $rootScope.$apply();
                }
            ]);
        });
    });

}