【问题标题】:index view is not binding to the controller in my angularjs app索引视图未绑定到我的 angularjs 应用程序中的控制器
【发布时间】:2017-07-26 17:31:11
【问题描述】:

我有以下定义我的角度应用程序的模块。

                        var ang = angular.module('mainapp', ['ngRoute']);


                    ang.config(['$routeProvider', '$locationProvider', function ($routeProvider, $locationProvider) {
                        $routeProvider.

                               when("/home", {
                                   templateUrl: "homepage.html",
                                   controller: "homeController"
                               }).
                               when("/quiz", {
                                   templateUrl: "quizpage.html",
                                   controller: "quizController"
                               }).

                               when("/", {
                                   templateUrl: "index.html",
                                   controller: "indexController"
                               });
                               //otherwise({ redirectTo: '/' });
                    }]);



                    ang.controller('indexController', function ($scope) {
                        $scope.btn = "Welcome"
                        $scope.Login = function () {
                            alert("Thanks ");
                            $location.path("home");
                        };
                    });

                    ang.controller('homeController', function ($scope) {
                        // initialize if you can
                        window.history.go(-1);
                        $scope.salutations = [{ name: "Mr", id: 1 }, { name: "Mrs", id: 2 }, { name: "Ms", id: 3 }, { name: "Jr", id: 4 }, { name: "Mister", id: 5 }, { name: "Dr", id: 6 }];

                        $scope.profile = {
                            name: "",
                            email: "",
                            contact: "",
                            division: "",
                            feedback: "",

                        };

                        $scope.submitInfo = function (profile) {
                            alert("Thanks " + profile.name + ". Lets get to the Quiz now.");
                             $location.path("quiz");
                        };
                    });

                    ang.controller('quizController', function ($scope) {
                        //initialize if you can
                        window.history.go(-1);
                        $scope.questions = [
                                       {
                                           "questionText": "Why is the sky blue?", "answers": [
                                            { "answerText": "blah blah 1", "correct": true },
                                            { "answerText": "blah blah 2", "correct": false },
                                            { "answerText": "blah blah 3", "correct": false }
                                           ]
                                       },
                                       {
                                           "questionText": "Why is the meaning of life?", "answers": [
                                            { "answerText": "blah blah 1", "correct": true },
                                            { "answerText": "blah blah 2", "correct": false },
                                            { "answerText": "blah blah 3", "correct": false }
                                           ]
                                       },
                                       {
                                           "questionText": "How many pennies are in $10.00?", "answers": [
                                            { "answerText": "1,000.", "correct": true },
                                            { "answerText": "10,000.", "correct": false },
                                            { "answerText": "A lot", "correct": false }
                                           ]
                                       },
                                       {
                                           "questionText": "What is the default program?", "answers": [
                                            { "answerText": "Hello World.", "correct": true },
                                            { "answerText": "Hello Sunshine.", "correct": false },
                                            { "answerText": "Hello my ragtime gal.", "correct": false }
                                           ]
                                       }
                        ];

                        $scope.answers = {};
                        $scope.correctCount = 0;
                        $scope.showResult = function () {
                            $scope.correctCount = 0;
                            var qLength = $scope.questions.length;
                            for (var i = 0; i < qLength; i++) {
                                var answers = $scope.questions[i].answers;
                                $scope.questions[i].userAnswerCorrect = false;
                                $scope.questions[i].userAnswer = $scope.answers[i];
                                for (var j = 0; j < answers.length; j++) {
                                    answers[j].selected = "donno";
                                    if ($scope.questions[i].userAnswer === answers[j].answerText && answers[j].correct === true) {
                                        $scope.questions[i].userAnswerCorrect = true;
                                        answers[j].selected = "true";
                                        $scope.correctCount++;
                                    } else if ($scope.questions[i].userAnswer === answers[j].answerText && answers[j].correct === false) {
                                        answers[j].selected = "false";
                                    }
                                }
                            }
                            //console.log($scope.answers);
                        };
                        $scope.submitQuiz = function (quiz) {
                            alert("Congrats.");
                             $location.path("index");
                        };
                    });

我想用欢迎按钮让用户登陆索引页面,点击后我想把用户带到主页,当用户在主页上填写信息时,它应该转到测验页面。

但应用程序根本没有将控制器绑定到索引页面。

        <!DOCTYPE html>
    <html data-ng-app="mainapp">
    <head>
        <title>WinPrizes</title>
    </head>
    <body >

        <div data-ng-controller="indexController">
            <button ng-click="Login()">{{btn}}</button>
        </div>
        <script src="Scripts/angular.min.js"></script>
        <script src="app/app.module.js"></script>
        <script src="app/main.js"></script>

    </body>
    </html>

打开索引页面时,按钮文本显示为 {{btn}}。这些不是部分模板。我只是想在导航用户点击每个页面中的按钮时切换到不同的 html 页面。

【问题讨论】:

  • 查看浏览器控制台日志是否有加载错误?由于插值未评估,因此框架在加载期间似乎出现错误。
  • 它显示错误:[$controller:ctrlreg] errors.angularjs.org/1.6.5/$controller/…

标签: javascript c# angularjs html


【解决方案1】:

您正在使用 ngRoute,但您没有在 index.html 中的 angulat.min.js 之后添加插件库 在 1.2 版以后的 angular-route.js 必须单独添加它不会进入主库.例如

<script src="https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.6.4/angular-route.js">
</script>

此外,您在所有控制器中都使用了 $location 服务,因此您必须将其作为依赖项传递给控制器​​函数。

您需要 index.html 上的 ng-view 指令才能使路由工作。请注意您的视图是部分视图(包含部分 html 代码)。另外为什么在控制器初始化时添加window.history.go(-1);?因为它总是会回到上一页 onload o 控制器。您应该仅在您要针对特定​​操作/事件调用的某个函数中添加此类代码。 这是您的代码版本的工作 plunker:

https://plnkr.co/edit/ADWf012Q7mTVBR3svPYb?p=preview

【讨论】:

  • 现在它给了我错误:localhost:52366/Scripts/angular-route.min.js 中第 8 行第 7 列的未处理异常。即使有网址。 0x800a138f - JavaScript 运行时错误:无法获取未定义或空引用的属性“模块”
  • @krrishna 检查更新的答案。下载 plunker,然后在 localhost 上检查 url 更改。此外,如果您不希望用户在未登录的情况下查看您应用中的任何 url,那么您可以创建 Angular 服务来跟踪登录状态并在该路由对象的解析属性中调用它。
  • 对我来说不幸的是它没有进入欢迎按钮 (index1.html) 页面。它只进入 index.html。你的 plunker 代码虽然有效。我将其复制到我的 VS 解决方案中。
  • 控制台有错误吗?您需要在 localhost 服务器上运行它才能使路由正常工作。使用 http-server npm 模块 (npmjs.com/package/http-server) 或括号编辑器的实时重载。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2018-06-01
  • 2016-09-02
  • 1970-01-01
  • 1970-01-01
  • 2023-03-21
  • 2015-03-07
  • 1970-01-01
相关资源
最近更新 更多