【问题标题】:ng-template not displaying views in jsFiddleng-template 未在 jsFiddle 中显示视图
【发布时间】:2015-12-06 21:47:10
【问题描述】:

我正在从here 学习 AngularJS 视图。该站点显示的示例应该输出以下内容:

但是,output 没有显示在 my fiddle 中:

         var mainApp = angular.module("mainApp", ['ngRoute']);
         mainApp.config(['$routeProvider', function($routeProvider) {
            $routeProvider.
            
            when('/addStudent', {
               templateUrl: 'addStudent.htm',
               controller: 'AddStudentController'
            }).
            
            when('/viewStudents', {
               templateUrl: 'viewStudents.htm',
               controller: 'ViewStudentsController'
            }).
            
            otherwise({
               redirectTo: '/addStudent'
            });
         }]);
         
         mainApp.controller('AddStudentController', function($scope) {
            $scope.message = "This page will be used to display add student form";
         });
         
         mainApp.controller('ViewStudentsController', function($scope) {
            $scope.message = "This page will be used to display all the students";
         });
			
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<h2>AngularJS <a href='http://www.tutorialspoint.com/angularjs/angularjs_views.htm'>ng-template example</a></h2>
      <div ng-app = "mainApp">
         <p><a href = "#addStudent">Add Student</a></p>
         <p><a href = "#viewStudents">View Students</a></p>
         <div ng-view></div>
         
         <script type = "text/ng-template" id = "addStudent.htm">
            <h2> Add Student </h2>
            {{message}}
         </script>
         
         <script type = "text/ng-template" id = "viewStudents.htm">
            <h2> View Students </h2>
            {{message}}
         </script>
      </div>

我相信,我已经正确选择了 LOAD TYPE

我在这里做错了什么?

【问题讨论】:

标签: javascript jquery html css angularjs


【解决方案1】:

为了让 ngRoute 正常工作,您必须添加:

<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.0rc1/angular-route.min.js"></script>

问题是由于缺少 ngRoute 模块引起的

working example

【讨论】:

    【解决方案2】:

    hrefs 你错了,前面应该有/。此外,您还没有在 Fiddle 中添加 angular-route.js

    标记

    <p><a href = "#/addStudent">Add Student</a></p>
    <p><a href = "#/viewStudents">View Students</a></p>
    

    Demo Fiddle

    【讨论】:

      猜你喜欢
      • 2020-04-16
      • 2013-02-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-09-04
      • 2013-04-04
      • 1970-01-01
      • 2017-01-20
      相关资源
      最近更新 更多