内嵌HTML ng模板不加载

问题描述:

我正在学习Thinkster的教程来学习Angular和MEAN Stack,但是由于某种原因,我无法让我的模板加载内嵌加载的HTML。有人能指出我要去哪里吗?代码着色不正确,页面在浏览器中加载时不显示任何内容。内嵌HTML ng模板不加载

我对此不甚了解?模板标签似乎不匹配?

<!doctype html> 
<html lang="en"> 
    <head> 
     <meta charset="UTF-8"> 
     <title>Flapper News</title> 
     <link href="http://maxcdn.bootstrapcdn.com/bootstrap/3.2.0/css/bootstrap.min.css" rel="stylesheet"> 
     <script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.3.10/angular.min.js"></script> 
     <script src="http://cdnjs.cloudflare.com/ajax/libs/angular-ui-router/0.2.10/angular-ui-router.js"></script> 
     <script src="app.js"></script> 
     <style>.glyphicon-thumbs-up {cursor:pointer}</style> 
    </head> 
    <body ng-app="flapperNews"><!--ng-controller="MainCtrl" style="margin-left:20px; margin-right:20px;"--> 
     <div class="row"> 
      <div class="col-md-6 col-md-offset-3"> 
       <ui-view></ui-view> 
      </div> 
     </div> 
     <!-- Start template --> 
     <script type="text/ng-template" id="/home.html"> 
      <div class="page-header"> 
       <h1>Flapper News</h1> 
      </div> 
      <div ng-repeat="post in posts | orderBy: '-upvotes'"> 
       <span class="glyphicon glyphicon-thumbs-up" ng-click="incrementUpvotes(post)"></span> 
       {{post.upvotes}} 
       <span style="font-size:20px; margin-left:10px;"> 
        <a ng-show="post.link" href="{{post.title}}"> 
         {{post.title}} 
        </a> 
        <span ng-hide="post.link"> 
         {{post.title}} 
        </span> 
       </span> 
      </div> 
      <form ng-submit="addPost()" style="margin-top:30px;"> 
       <h3>Add a new post</h3> 
       <div class="form-group"> 
        <input type="text" 
         class="form-control" 
         placeholder="Title" 
         ng-model="title"></input> 
       </div> 
       <div class="form-group"> 
        <input type="text" 
         class="form-control" 
         placeholder="Link" 
         ng-model="link"></input> 
       </div> 
       <button type='submit' class="btn btn-primary">Post</button>  
      </form> 
     </script> 
     <!-- end template --> 
     <!-- start template --> 
     <script type="text/ng-template" id="/posts.html"> 
      <div class="page-header"> 
      <h3> 
       <a ng-show="post.link" href="{{post.link}}"> 
        {{post.title}} 
       </a> 
       <span ng-hide="post.link"> 
        {{post.title}} 
       </span> 
      </h3> 
      </div> 
      <div ng-repeat="comment in post.comments | orderBy:'-upvotes'"> 
      <span class="glyphicon glyphicon-thumbs-up" ng-click="incrementUpvotes(comment)"></span> 
      {{comment.upvotes}} - by {{comment.author}} 
      <span style="font-size:20px; margin-left:10px;"> 
       {{comment.body}} 
      </span> 
      </div> 
     </script> 
     <!-- end template --> 
    </body> 
</html> 

我得到这个错误:

Error: $injector:modulerr 
Module Error 

这里是我的app.js

/*global angular*/ 
var app = angular.module('flapperNews', []); 
angular.module('flapperNews', ['ui.router']); 
app.factory('posts',[function(){ 
    var o = { 
     posts: [] 
    }; 
    return o; 
}]); 

app.config([ '$stateProvider', '$urlRouterProvider', 
    function($stateProvider, $urlRouterProvider) { 

     $stateProvider 
     .state('home', { 
      url: '/home', 
      templateUrl: '/home.html', 
      controller: 'MainCtrl' 
     }); 
     .state('posts', { 
      url: '/posts/{id}', 
      templateUrl: '/posts.html', 
      controller: 'PostsCtrl' 
     }); 
     $urlRouterProvider.otherwise('home'); 
    }]); 

app.controller('MainCtrl', ['$scope','posts', function($scope, posts) { 
    $scope.posts = posts.posts; 
    $scope.posts = [ 
     {title: 'post 1', upvotes: 12}, 
     {title: 'post 2', upvotes: 14}, 
     {title: 'post 3', upvotes: 16}, 
     {title: 'post 4', upvotes: 11}, 
     {title: 'post 5', upvotes: 1} 
     ]; 
    $scope.addPost = function(){ 
     if (!$scope.title || $scope.title === '') {return;} 
     $scope.posts.push({ 
      title: $scope.title, 
      link: $scope.link, 
      upvotes: 0, 
      comments: [ 
       {author: 'Joe', body: 'Cool post!', upvotes: 0}, 
       {author: 'Bob', body: 'Great idea but everything is wrong!', upvotes: 0} 
      ] 
     }); 
     $scope.title = ''; 
     $scope.link = ''; 
    } 
    $scope.incrementUpvotes = function(post){ 
     post.upvotes += 1; 
    } 
}]); 

app.controller('PostsCtrl', [ '$scope', '$stateParams', 'posts', 
    function($scope, $stateParams, posts){ 
     $scope.post = posts.posts[$stateParams.id]; 
    }]); 
+1

更改您使用的ui路由器地址是无效的。这是新的http://cdnjs.cloudflare.com/ajax/libs/angular-ui-router/0.2.18/angular-ui-router.js – Konkko

+0

@Konkko这仍然不能解决问题。 HTML标记不起作用 – bdeo

+0

您可以将代码发布到您定义模块的位置吗? – bumpy

检查你的角度-UI-router.js库地址它cloudfare不CloudFlare的。

编辑

从家乡

.state('home', { url: '/home', templateUrl: '/home.html', controller: 'MainCtrl' });

删除不必要的;和删除行angular.module('flapperNews', ['ui.router']); 添加ui.router到第一线var app = angular.module('flapperNews', ['ui.router']);

要工作JS:

var app = angular.module('flapperNews', ["ui.router"]); 
app.factory('posts',[function(){ 
    var o = { 
     posts: [] 
    }; 
    return o; 
}]); 

app.config([ '$stateProvider', '$urlRouterProvider', 
    function($stateProvider, $urlRouterProvider) { 

     $stateProvider 
     .state('home', { 
      url: '/home', 
      templateUrl: '/home.html', 
      controller: 'MainCtrl' 
     }) 
     .state('posts', { 
      url: '/posts/{id}', 
      templateUrl: '/posts.html', 
      controller: 'PostsCtrl' 
     }); 
     $urlRouterProvider.otherwise('home'); 
    }]); 

app.controller('MainCtrl', ['$scope','posts', function($scope, posts) { 
    $scope.posts = posts.posts; 
    $scope.posts = [ 
     {title: 'post 1', upvotes: 12}, 
     {title: 'post 2', upvotes: 14}, 
     {title: 'post 3', upvotes: 16}, 
     {title: 'post 4', upvotes: 11}, 
     {title: 'post 5', upvotes: 1} 
     ]; 
    $scope.addPost = function(){ 
     if (!$scope.title || $scope.title === '') {return;} 
     $scope.posts.push({ 
      title: $scope.title, 
      link: $scope.link, 
      upvotes: 0, 
      comments: [ 
       {author: 'Joe', body: 'Cool post!', upvotes: 0}, 
       {author: 'Bob', body: 'Great idea but everything is wrong!', upvotes: 0} 
      ] 
     }); 
     $scope.title = ''; 
     $scope.link = ''; 
    } 
    $scope.incrementUpvotes = function(post){ 
     post.upvotes += 1; 
    } 
}]); 

app.controller('PostsCtrl', [ '$scope', '$stateParams', 'posts', 
    function($scope, $stateParams, posts){ 
     $scope.post = posts.posts[$stateParams.id]; 
    }]); 

HTML:

+0

这仍然不起作用 – bdeo

+0

@bdeo查看编辑答案。这应该在这里工作是一个工作Plunker http://plnkr.co/edit/u42STTpQVYATSydzMYeA – Konkko

+0

所以这似乎工作,我可以看到它在沉重,但我想现在是一个C9的问题,因为C9显示不正确。 @Konkko – bdeo