2016-01-28 56 views
0

我知道這個問題可能很愚蠢,但是在遍歷整個stackoverflow和其他博客以及視頻教程後,我沒有別的選擇,只能在stackoverflow上詢問這個問題。 我一直在通過在線教程學習Angular.js。Angular.js ui-sref不會創建超鏈接

我有2個文件。 index.html和app.js

這是我的代碼的一些快照。

美國配置部分內app.js內的index.html

angular.module('flapperNews', ['ui.router']).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')}]); 

內聯模板(1-home.html做爲,2- posts.html)

<ui-view></ui-view> 
<script type="text/ng-template" id="/home.html"> 
    <!--home.html part--> 
</script> 
<script type="text/ng-template" id="/posts.html"> 
    <!--posts.html part--> 
</script> 

inside home.html我在評論上有超鏈接。

<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.link}}"> 
     {{post.title}} 
    </a> 
    <span ng-hide="post.link"> 
     {{post.title}} 
    </span> 
    <span> 
     <a ui-sref="/posts/{{$index}}">Comments</a> 
    </span> 
    </span> 
</div> 

現在,當我在我的本地運行此Web應用程序是顯示我的評論超級鏈接的這種呈現的HTML,但它不提供我任何導航(超鏈接無法正常工作)。

<span> 
    <a ui-sref="/posts/0">Comments</a> 
</span> 

現在,我有兩個問題:

  1. 出了什麼UI的SREF part.When我使用的href,而不是UI-SREF則可以正常使用fine.Why會出現這種情況?
  2. 我一直在使用home.html和posts.html的內聯模板,但是如果我將它們製作成單獨的文件,那麼我的整個應用程序就會被破壞。爲什麼?

任何幫助將是值得讚賞的。

+0

您是否檢查了瀏覽器開發控制檯?使用模板作爲單獨文件時記錄的任何錯誤? – Raghu

+0

它顯示了這個:http://localhost/home.html加載資源失敗:對於問題1或2,服務器的響應狀態爲404(未找到) – unlucy7735

回答

2

1)你已經使用ui-sref的方法是什麼原因造成它在你的應用程序break.Try此,

<a ui-sref="posts({ id: $index })">Comments</a> 

2)假設你的新模板文件是作爲同一級別您的index.html和app.js您不需要提及templateUrl: '/posts.html',只需使用templateUrl: 'posts.html'即可。

+0

? – unlucy7735

+0

它解決了問題1。謝謝man.Now for question no 2?問題編號:2的 – unlucy7735

+0

,顯示如下:localhost/home.html加載資源失敗:服務器響應狀態爲404(未找到) – unlucy7735