【问题标题】:Website using angular js and firebase Error: [$injector:modulerr]网站使用 Angular js 和 firebase 错误:[$injector:modulerr]
【发布时间】:2017-11-29 05:50:30
【问题描述】:

我的 Angular js 有问题,它会给我这个错误print screen of the error in the console log

我正在尝试通过 firebase 检索用户名,然后使用 angular js 将其打印到网页上。

这是我的 html 中的代码

<header>
    <div class="header-content">
        <div class="header-content-inner">

            <!--<h1 id="homeHeading">Your Favorite Source of Free Bootstrap Themes</h1>!-->
            <div data-ng-app="myApp" data-ng-controller="myCtrl">//fix this

                <p>welcome, {{firstname}}</p>
                <hr>
                <p>Start Bootstrap can help you build better websites using the Bootstrap CSS framework! Just download your template and start going, no strings attached!</p>
                <a href="#about" class="btn btn-primary btn-xl page-scroll">Find Out More</a>
            </div>
        </div>
    </div>
</header>

这是我的javascript

<script>

    $(document).ready(function(){
        var app = angular.module('myApp',['ngRoute']);

        app.controller('myCtrl', function($scope){
            var userId = firebase.auth().currentUser.uid;

            console.log(userId);
            $scope.firstname = firebase.database().ref('users/' + userId).once('value').then(function(snapshot){
                console.log(snapshot.val().firstname);
                return snapshot.val().firstname;
            });
        });

        firebase.auth().onAuthStateChanged(firebaseUser => {
            if(firebaseUser){
                console.log(firebaseUser);
            }else{
                window.location.replace("index.html");
                console.log('not logged in');
            }
        });
        $("#btnLogout").click(function(){
            firebase.auth().signOut();
            window.location.replace("index.html");
        });
    });
</script>

【问题讨论】:

  • 您的 javascript 代码不完整。你错过了上面的东西吗?最后是右括号,会产生错误。
  • 我没有添加我所有的 javascript 代码,因为其余的只是无关的 firebase 东西
  • 我不需要 firebase 的东西。问题是,您的代码运行良好(当然,如果我删除了最后一个括号)。因此,如果没有更多代码,我实际上无法为您提供帮助。我的猜测是,你的 angular.module 没有被调用。
  • 哦,好的,我会编辑我的帖子,并为您提供完整的 javascript。
  • 还是 js 新手,但我的语法现在还好吗?我的 Angular js 仍然出现同样的错误,但没有语法错误

标签: jquery angularjs firebase firebase-realtime-database firebase-authentication


【解决方案1】:

问题是,您在准备好文档时调用 angular.module。文档准备好,显然,在 Angular 查找您的模块后调用 get 。

工作的javascript:

<script>
        var app = angular.module('myApp',[]);

        app.controller('myCtrl', function($scope){
            var userId = firebase.auth().currentUser.uid;

            console.log(userId);
            $scope.firstname = firebase.database().ref('users/' + userId).once('value').then(function(snapshot){
                console.log(snapshot.val().firstname);
                return snapshot.val().firstname;
            });
        });

        firebase.auth().onAuthStateChanged(firebaseUser => {
            if(firebaseUser){
                console.log(firebaseUser);
            }else{
                window.location.replace("index.html");
                console.log('not logged in');
            }
        });
        $("#btnLogout").click(function(){
            firebase.auth().signOut();
            window.location.replace("index.html");
        });
</script>

【讨论】:

    【解决方案2】:

    我认为你没有添加 ngRoute。添加此代码并再次检查。

    <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.0rc1/angular-route.min.js"></script>
    
    var app = angular.module('myapp', ['ngRoute']);
    

    【讨论】:

    • @Mr. Munchkin 试试这个解决方案,这会对你有所帮助。
    • 您是否在 index.html 文件中添加了链接?
    • 是的,javascript实际上在我的html中,我打算稍后在修复错误时清理它,因为我很懒,哈哈
    猜你喜欢
    • 2015-10-29
    • 2017-04-10
    • 2014-01-29
    • 1970-01-01
    • 1970-01-01
    • 2020-07-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多