2016-11-14 10 views
2

HTMLファイルを分離して再利用できるようにしましたが、残念ながら動作しません。グーグルで回ってチュートリアルに従ったが、何も動かなかった、多分私は何かを見過ごした。私の問題の概要を明らかにするために、モジュール全体が追加されました。おそらくそれに何らかのエラーがあるかもしれません。コードはindex.htmlで完璧に動作しますが、別のhtmlファイルに分割したいと考えています。以下の例は、 "wheretobuy.html"ファイルで実行する必要があります。これで私を助けることができる人。HTMLページを分離しようとしています。角度

var app = angular.module("myApp", ["ngRoute", "slideshow", "json", "accessoires", "wheretobuy"]); 
 
app.config(function($routeProvider) { 
 
    // $locationProvider.html5Mode(true); 
 
    $routeProvider 
 
    .when('/', { 
 
     templateUrl: 'pages/home.html', 
 
     controller: 'HomeController' 
 
    }) 
 
    .when('/accessories', { 
 
     templateUrl: 'pages/accessories.html', 
 
     controller: 'AccessoriesController' 
 
    }) 
 
    .when('/wheretobuy', { 
 
     templateUrl: 'pages/wheretobuy.html', 
 
     controller: 'WheretobuyController' 
 
    }) 
 
    .when('/service', { 
 
     templateUrl: 'pages/service.html', 
 
     controller: 'ServiceController' 
 
    }) 
 
    .when('/forum', { 
 
     templateUrl: 'pages/forum.html', 
 
     controller: 'ForumController' 
 
    }) 
 
    .otherwise({ 
 
     redirectTo: '/' 
 
    }); 
 
}); 
 

 
app.controller('HomeController', function($scope) { 
 
    $scope.message = 'Hello from HomeController'; 
 
}); 
 
app.controller('AccessoriesController', function($scope) { 
 
    $scope.message = 'Hello from AccessoriesController'; 
 
}); 
 
app.controller('WheretobuyController', function($scope) { 
 
    $scope.message = 'WheretobuyController'; 
 
}); 
 
app.controller('ServiceController', function($scope) { 
 
    $scope.message = 'Hello from ServiceController'; 
 
}); 
 
app.controller('ForumController', function($scope) { 
 
    $scope.message = 'Hello from ForumController'; 
 
});
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script> 
 

 
<body> 
 

 
    <script type="text/ng-template" id="pages/wheretobuy.html"> 
 
    <div class="row" ng-controller="TabController"> 
 
     <div class="col-md-2"> 
 
     <ul class="nav nav-pills nav-stacked"> 
 
      <li class="{active: isSet}"> 
 
      <a href ng-click="setTab(1)" class="country">Asia</a> 
 
      </li> 
 

 
      <li class="{active: isSet}"> 
 
      <a href ng-click="setTab(2)" class="country">Europe</a> 
 
      </li> 
 

 
      <li class="{active: isSet}"> 
 
      <a href ng-click="setTab(3)" class="country">South-America</a> 
 
      </li> 
 

 
      <li class="{active: isSet}"> 
 
      <a href ng-click="setTab(4)" class="country">Oceania</a> 
 
      </li> 
 
     </ul> 
 
     </div> 
 

 
     <div class="collapse navbar-collapse"> 
 
     <ul class="nav navbar-nav"> 
 
      <li><a href="#/">Products</a> 
 
      </li> 
 
      <li><a href="#/accessories">Accessories</a> 
 
      </li> 
 
      <li><a href="#/wheretobuy">Where to buy</a> 
 
      </li> 
 
      <li><a href="#/service">Service</a> 
 
      </li> 
 
      <li><a href="#/forum">Forum</a> 
 
      </li> 
 
     </ul> 
 
     </div> 
 
</body> 
 
</script>

+0

に一致するようにtext/ng-templateタイプを使用して作成しました私はあなたがルートを登録するのを忘れて、あなたのタグhrefがちょっと変わったように見えます。 #/ pagesnamesを参照していますが、この種のパスをどこにも登録していないからです。 –

+0

この場合、ルートファイルは "wheretobuy.html"です。この場合はパス名 "pages"を削除する必要がありますか? –

+0

が見つかりました[this](http://stackoverflow.com/questions/28788443/href-with-routeprovider-loads-a-new-page-instead-of-binding-the-view)。あなたのhrefに#記号を使用しないように見える –

答えて

2

以下の解決策を確認してください、ここでのキーポイントは、ng-appディレクティブを使用して、アプリケーションのブートストラップ$routeProvider

  • を使用してルートを設定

    1. です。

    2. テンプレートをレンダリングするビューでng-viewディレクティブを使用する。 HTMLテンプレートのIDを指定

    3. は、私はわからないけどに見えますよ関連付けられtemplateUrlプロパティの値

    var app = angular.module("myApp", ["ngRoute"]); 
     
    
     
    app.config(function($routeProvider) { 
     
        $routeProvider 
     
        .when('/', { 
     
         templateUrl: 'pages/home.html', 
     
         controller: 'HomeController' 
     
        }) 
     
        .when('/accessories', { 
     
         templateUrl: 'pages/accessories.html', 
     
         controller: 'AccessoriesController' 
     
        }) 
     
        .when('/wheretobuy', { 
     
         templateUrl: 'pages/wheretobuy.html', 
     
         controller: 'WheretobuyController' 
     
        }) 
     
        .when('/service', { 
     
         templateUrl: 'pages/service.html', 
     
         controller: 'ServiceController' 
     
        }) 
     
        .when('/forum', { 
     
         templateUrl: 'pages/forum.html', 
     
         controller: 'ForumController' 
     
        }) 
     
        .otherwise({ 
     
         redirectTo: '/' 
     
        }); 
     
    }); 
     
    
     
    app.controller('HomeController', function($scope) { 
     
        $scope.message = 'Hello from HomeController'; 
     
    }); 
     
    
     
    app.controller('AccessoriesController', function($scope) { 
     
        $scope.message = 'Hello from AccessoriesController'; 
     
    }); 
     
    
     
    app.controller('WheretobuyController', function($scope) { 
     
        $scope.message = 'Hello from WheretobuyController'; 
     
    }); 
     
    
     
    app.controller('ServiceController', function($scope) { 
     
        $scope.message = 'Hello from ServiceController'; 
     
    }); 
     
    
     
    app.controller('ForumController', function($scope) { 
     
        $scope.message = 'Hello from ForumController'; 
     
    });
    ul { 
     
        list-style-type: none; 
     
        padding: 0; 
     
        margin: 0; 
     
    } 
     
    ul li { 
     
        padding: 5px; 
     
    }
    <script src="https://code.angularjs.org/1.5.8/angular.js"></script> 
     
    <script src="https://code.angularjs.org/1.5.8/angular-route.js"></script> 
     
    
     
    <div ng-app="myApp"> 
     
        <ul> 
     
        <li> 
     
         <a href="#/">Products</a> 
     
        </li> 
     
        <li> 
     
         <a href="#/accessories">Accessories</a> 
     
        </li> 
     
        <li> 
     
         <a href="#/wheretobuy">Where to buy</a> 
     
        </li> 
     
        <li> 
     
         <a href="#/service">Service</a> 
     
        </li> 
     
        <li> 
     
         <a href="#/forum">Forum</a> 
     
        </li> 
     
        </ul> 
     
        <ng-view></ng-view> 
     
    
     
        <script type="text/ng-template" id="pages/home.html"> 
     
        <h1 ng-bind="message"></h1> 
     
        </script> 
     
    
     
        <script type="text/ng-template" id="pages/accessories.html"> 
     
        <h1 ng-bind="message"></h1> 
     
        </script> 
     
    
     
        <script type="text/ng-template" id="pages/wheretobuy.html"> 
     
        <h1 ng-bind="message"></h1> 
     
        </script> 
     
    
     
        <script type="text/ng-template" id="pages/service.html"> 
     
        <h1 ng-bind="message"></h1> 
     
        </script> 
     
    
     
        <script type="text/ng-template" id="pages/forum.html"> 
     
        <h1 ng-bind="message"></h1> 
     
        </script> 
     
    </div>

  • 関連する問題