2016-09-26 11 views
1

以下のコードは、stackoverflowコンソールで正常に動作しますが、ブラウザでは正常に動作しません。 全員が私のために働いていません。あなたの助けをよろしくお願いします。 angularJSを始めました。angularjsルーティングテンプレートが機能しない

<!DOCTYPE html> 
<html> 
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.4.8/angular.min.js"></script> 
<script src="//ajax.googleapis.com/ajax/libs/angularjs/1.4.8/angular-route.js"></script> 

<body ng-app="myApp"> 

<p><a href="#/">Main</a></p> 

<a href="#banana">Banana</a> 
<a href="#tomato">Tomato</a> 

<p>Click on the links to change the content.</p> 

<p>The HTML shown in the ng-view directive are written in the template property of the $routeProvider.when method.</p> 

<div ng-view></div> 

<script> 
var app = angular.module("myApp", ["ngRoute"]); 
app.config(function($routeProvider) { 
    $routeProvider 
    .when("/", { 
        template : <h1>Main</h1><p>Click on the links to change this content</p> 
    }) 
    .when("/banana", { 
        template : <h1>Banana</h1><p>Bananas contain around 75% water.</p> 
    }) 
    .when("/tomato", { 
        template : <h1>Tomato</h1><p>Tomatoes contain around 95% water.</p> 
    }); 
}); 
</script> 

</body> 
</html> 
+1

つ私に飛び出すもの:1)あなたが不足している 'ます。https:'あなたから ' angle-route.js'スクリプトリファレンスと2) 'template'値はサンプルコードの文字列であるため引用符で囲む必要があります。 – Lex

+0

仕事やルート、アンカーとは何ですか?あなたはng-href – dwbartz

答えて

1
あなたが引用符でテンプレートを囲む必要があり

var app = angular.module("myApp", ["ngRoute"]); 
app.config(function($routeProvider) { 
    $routeProvider 
    .when("/", { 
     template :"<h1>Main</h1><p>Click on the links to change this content</p>" 
    }) 
    .when("/banana", { 
     template : "<h1>Banana</h1><p>Bananas contain around 75% water.</p>" 
    }) 
    .when("/tomato", { 
     template : "<h1>Tomato</h1><p>Tomatoes contain around 95% water.</p>" 
    }); 
}); 

WORKING DEMO

+0

を使っているはずです。まだ動かない。 "/"テンプレートを表示しますが、他の2つのホイールはクリックしません。 – laput

+0

@laputデモをチェックしましたか?私は – Sajeetharan

+0

を追加しました。スタックオーバーフローコンソールでも同じコードが動作します。私のブラウザでは動作しません。私はApache Webサーバーを実行しています。だからそれはコード以外のものでなければならないと思っています。 – laput

関連する問題