2017-02-05 26 views
1

私はチュートリアルに従っているときに角度ルーティングの例を見ました。私はチュートリアルとして試しましたが、うまくいきません。しかし、私はチュートリアルからコードをコピーすると完全に動作します。誰もがこれで私を助けることができます。角度ルーティングは機能しません

これが私のメインページ

<div ng-controller="routingCtrl"> 
    <div> 
     <ul> 
      <li><a href="#home">Home</a> </li> 
      <li><a href="#other">Oher</a></li> 
      <li><a href="#contact">Contact</a></li> 
     </ul> 
     <div> 
      <p>{{message}}</p> 
     </div> 
     <ng-view></ng-view> 
    </div> 
</div> 

であり、これはscript.js

var routingApp = angular.module('sampleRouting', ['ngRoute']) 
       .controller('routingCtrl', function ($scope) { 
        $scope.message = "Main Page"; 
       }) 
       .config(function ($routeProvider, $locationProvider) { 
        $routeProvider 
        .when('/', { 
         templateUrl: 'home.html', 
         controller: 'homeCtrl' 
        }) 
        .when('/other', { 
         templateUrl: 'other.html', 
         controller: 'otherCtrl' 
        }) 
        .when('/contact', { 
         templateUrl: 'contact.html', 
         controller: 'contactController' 
        }) 
        .otherwise({ redirectTo: '/' }); 
        }) 
        .controller('homeCtrl', function ($scope) { 
         $scope.homeMessage = "Welcome to home !" 
        }) 
        .controller('otherCtrl', function ($scope) { 
         $scope.otherMessage = "Welcome to other !" 
        }) 
        .controller('contactController', function ($scope) { 
         $scope.contactMessage = "Welcome to Contacts !" 
        }) 

と呼ばれる私のスクリプトファイルであり、これはother.htmlある

<div> 
Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum. 

Why do we use it? 
It is a long established fact that a reader will be distracted by the readable content of a page when looking at its layout. The point of using Lorem Ipsum is that it has a more-or-less normal distribution of letters, as opposed to using 'Content here, content here', making it look like readable English. Many desktop publishing packages and web page editors now use Lorem Ipsum as their default model text, and a search for 'lorem ipsum' will uncover many web sites still in their infancy. Various versions have evolved over the years, sometimes by accident, sometimes on purpose (injected humour and the like). 

他の2 home.htmlcontact.htmlも、このようなランダムなテキストが含まれています。

問題はビューはの初めにindex.htmlをにロードされたhome.htmlですが、私はその他連絡先にコンテンツをハイパーリンクをクリックしたときにチャンスではないでしょう。 home.htmlがロードされているので、ngRouteはうまく動作すると思います。しかし、他の2つは動作しません。誰も私がこの問題を解決するのを助けることができますか?どうもありがとう。

答えて

0

角度1.6を使用しているため、チュートリアルで古いバージョンの角度が使用されている可能性があります。そのため、コードをコピーすると効果があります。

あなたはこれを変更することにより、ケースであることを確認することができます

<li><a href="#other">Oher</a></li> 

<li><a href="#!other">Oher</a></li> 

に1.6のデフォルトのハッシュプレフィックスの変化がありました。 この問題を解消するには、AngularJS: ngRoute Not Working

を参照してください。チュートリアルのバージョンに戻したい場合は、リンクからオプション3を使用し、手動でハッシュプレフィックスを設定する必要があります。お使いの設定で

app.config(['$locationProvider', function($locationProvider) { 
    $locationProvider.hashPrefix(''); 
}]); 
+0

おかげで多くを除去するため、次の試してください。これは動作します –

+0

私のスマートなテーブルの何が間違っているか教えてもらえますか?ルーティング以外はすべて正常に動作しています [これをチェック](https://plnkr.co/edit/hb3gcgp6snBPLCA9I8qw?p=preview) –

+0

編集:上記の問題は解決しました。ありがとう –

0

は削除し、あなたのHTML内#

$locationProvider.html5Mode(true); 

でルーティング#

<li><a href="home">Home</a> </li> 
<li><a href="other">Oher</a></li> 
<li><a href="contact">Contact</a></li> 
+0

私はこれを試しました。しかし、動作しませんでした。 とにかく助けてくれてありがとう –

+0

この作業を行うには、サーバー側のコードを設定する必要があります。この回答を見るhttp://stackoverflow.com/questions/42046583/cant-get-route-angular/42046776#42046776 –

関連する問題