简体   繁体   English

无法从Meteor模板调用Angular Controller

[英]Unable to call Angular Controller from Meteor template

I have updated my Meteor project from router to Iron-router; 我已经将Meteor项目从路由器更新为Iron-router; I have created client / server side routing using Iron-router. 我已经使用Iron-router创建了客户端/服务器端路由。

After updating to the iron router I am having a issue calling the LoginController when I hit the submit button it does not go to doLogin function. 更新到铁路由器后,当我按下“提交”按钮时,我遇到了调用LoginController的问题,它无法进入doLogin函数。 I am handing all the functionality in angular.doLogin function handles the login functionality. 我将所有的功能交给angular.doLogin函数来处理登录功能。

client side route : 客户端路由:

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

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

login template: 登录模板:

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

login controller: 登录控制器:

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

I was able to fix the issue,Following solution helped me : https://github.com/Urigo/angular-meteor/issues/48 我能够解决此问题,以下解决方案帮助了我: 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();
                }
            ]);
        });
    });

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM