[英]AngularJS searchbox directive or how to avoid code duplication in controller
[英]How to avoid duplication between templates in an AngularJS app?
假設我網站中的所有視圖都應包含一些共享內容,例如,頂部的導航欄。 如何在不跨所有視圖復制導航欄標記的情況下實現這一目標?
我正在尋找在AngularJS中執行此操作的最標准方法。
您可以使索引頁面具有導航欄和頁腳,然后具有ng-view div。
您需要包括ngRoute模塊,以及鏈接到它的腳本標簽。
您將有一個這樣的div。
<div ng-view=""></div>
您將擁有一個看起來像這樣的partialRoutes.js文件。
myApp.config(function($routeProvider){
$routeProvider
.when('/',{
templateUrl: './partials/things.html'
})
.when('/stuff',{
templateUrl: './partials/stuff.html'
})
.when('/otherstuff',{
templateUrl: './partials/otherstuff.html'
})
.otherwise({
redirectTo: '/',
})
});
當您包含ngRoute時,它將看起來像這樣。
var myApp = angular.module('myApp',['ngRoute']); 這是ngRoute的文檔。 希望我能幫上忙。
您可能想檢查一下: https ://scotch.io/tutorials/angular-routing-using-ui-router“它提供了與ngRoute不同的方法,因為它根據應用程序的狀態更改應用程序視圖,而不是只是路線網址。”
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.