3

これは、デフォルトの noemployee.html パーシャルを持つ基本的なセットアップです: ng-view として

ここに画像の説明を入力

Index.html コンテンツ:

 <div id="container" ng-controller="EmployeeCtrl">

 <!-- Side Menu -->
 <span id="smenuSpan">
 <ul id="thumbList">
 <li ng-repeat="employee in employees | filter:categories">
 <a href="Employee/{{employee.id}}"><img  class="smallImage" ng-src="content/app/images/{{employee.image}}" alt="{{employee.description}}"></a>
 </li>
 </ul>
 </span>

 <!-- Content -->
 <span id="contentSpan">
 <div ng-view></div>    
 </span>

 </div>

私のルートプロバイダー:

var EmployeeModule = angular.module('EmployeeModule',  [], function($routeProvider, $locationProvider) {

$routeProvider.when('/', { templateUrl: 'content/app/partials/noemployee.html', controller: EmployeeModule.EmployeeCtrl });
$routeProvider.when('Employee/:id', { templateUrl: 'content/app/partials/employee.html', controller: EmployeeModule.EmployeeCtrl });
$routeProvider.otherwise({ redirectTo: '/' });

$locationProvider.html5Mode(true);

私のコントローラー:

function EmployeeCtrl($scope, $http, $routeParams, $timeout) {

    $scope.employees = [
{ "id": 1, "category": "ones", "image": "person1.jpg",  "description": "person 1 description", name:"Jane Smith" },
{ "id": 2, "category": "twos", "image": "person2.jpg", "description": "person 2 description", name: "Mark Sharp" },
{ "id": 3, "category": "threes", "image": "person3.jpg", "description": "person 3 description", name: "Kenny Suave" },
{ "id": 4, "category": "fours", "image": "person4.jpg", "description": "person 4 description", name: "Betty Charmer" },
{ "id": 5, "category": "fives", "image": "person5.jpg", "description": "person 5 description", name: "John Boss" }
];

$scope.employeesCategories = [];
$scope.currentEmployee = {};
$scope.params = $routeParams;

$scope.handleEmployeesLoaded = function (data, status) {
    //$scope.images = data;
    // Set the current image to the first image in images
    $scope.currentEmployee = _.first($scope.employees);
    // Create a unique array based on the category property in the images objects
    $scope.employeeCategories = _.uniq(_.pluck($scope.employees, 'category'));
}

$scope.fetch = function () {
    $http.get($scope.url).success($scope.handleEmployeesLoaded);
};

$scope.setCurrentEmployee = function (employee) {
    $scope.currentEmployee = employee;
};

// Defer fetch for 1 second to give everything an opportunity layout
$timeout($scope.fetch, 1000);

}

所見:

  1. 現在、従業員をクリックしても「Employee/??」は表示されません。がアドレス バーのパスに追加されます [これは私にとって犯罪ではありません]。

  2. 「$locationProvider.html5Mode(true);」をコメントアウトすると、デフォルトのローカルホストは「http://localhost:31219/#/」になり、従業員をクリックするとアドレスバーに「http://localhost: 」と表示されます。 31219/Employee/1 '、ページは 404 エラー ページに移動します。

私はここで何かをろくでなしにしているので、解決策が非常に単純であることを知っています。

目標:

  1. アドレスバーにハッシュタグを付けないようにしたいです。
  2. 従業員/IDがアドレスバーに表示されないことはいいことですが、必要ありませんが、それなしでは部分的に変更できないと思います. そして、当然

  3. 従業員をクリックすると、パーシャルが「employee.html」ページに変更されます。

このコードのどこが間違っているのか、誰にもわかりますか?

前もって感謝します!

4

2 に答える 2

2

解決:

  1. img href に「#/」を入れる必要がありました --> href="#/Employee/{{employee.id}}"
  2. 「$locationProvider.html5Mode(true);」をコメントアウトします。

補足として、これらの厄介なハッシュタグなしでこれを機能させる方法を知っていたらよかったのにと思います。アイデアはありますか?

html5mode を使用するには、サーバーが無効なルートのメイン アプリ インデックス ファイルを提供する必要があります。

たとえば、サーバー側のコードが /api/employees、/api/employees/:id などのパスで CRUD 操作を処理するとします。

静的コンテンツ、画像、html、css、js などを提供します。

それ以外の場合は 404 になる他の要求については、404 で応答する代わりに、200 コードで応答し、index.html ファイルを提供する必要があります。

このようにして、非静的および非サーバー側のルートは、Angular アプリによって処理されます。

これは、このページの Angular ガイドに記載されています: http://docs.angularjs.org/guide/dev_guide.services.$location

末尾の「サーバー側」のコメントに注意してください。

HTMLリンク書き換え

HTML5 履歴 API モードを使用する場合、ブラウザごとに異なるリンクが必要になりますが、次のような通常の URL リンクを指定するだけで済みます。<a href="/some?foo=bar">link</a>

ユーザーがこのリンクをクリックすると:

従来のブラウザでは、URL が /index.html#!/some?foo=bar に変わります

最新のブラウザでは、URL が /some?foo=bar に変更されます。次のような場合、リンクは書き換えられません。代わりに、ブラウザは元のリンクへのページ全体のリロードを実行します。

ターゲット要素を含むリンク 例:<a href="/ext/link?a=b" target="_self">link</a>

別のドメインに移動する絶対リンク 例:<a href="http://angularjs.org/">link</a>

base が定義されている場合に別の base パスにつながる「/」で始まるリンク
例:<a href="/not-my-base/link">link</a>

サーバ側

このモードを使用するには、サーバー側で URL を書き換える必要があります。基本的に、アプリケーションのエントリ ポイント (index.html など) へのすべてのリンクを書き換える必要があります。

于 2013-01-28T20:16:07.377 に答える
1

これが問題でした:

<a href="Employee/{{employee.id}}"><img  class="smallImage" ng-src="content/app/images/{{employee.image}}" alt="{{employee.description}}"></a>

解決:

  1. img hrefに「#/」を入れる必要がありました-> href = "#/ Employee / {{employee.id}}"
  2. コメントアウト'$locationProvider.html5Mode(true);'

ちなみに、これらの厄介なハッシュタグなしでこれを機能させる方法を知っていればいいのにと思います。誰かアイデアはありますか?

于 2013-01-08T18:48:27.707 に答える