我在我的應用程序中有四到五個選項卡視圖。因此,點擊每個標籤,我會根據標籤選擇來顯示內容。使用ng-route進行部分視圖vs ng-show/ng-hide我應該使用哪一個?
我嘗試了兩種方法之一是ng-route
,另一個是ng-show/ng-hide
。我覺得ng-show/ng-hide
擅長性能水平,我想我應該遵循相同的。以下是我試過
第一種方法NG-路線
main.php
var testApp = angular.module("testApp", ["ngRoute"]);
testApp.config(function ($routeProvider) {
$routeProvider.when("/tab1", {
templateUrl: "tab1.php"
});
$routeProvider.when("/tab2", {
templateUrl: "tab2.php"
});
$routeProvider.when("/tab3", {
templateUrl: "tab3.php"
});
$routeProvider.when("/tab4", {
templateUrl: "tab4.php"
});
$routeProvider.otherwise({
templateUrl: "tab1.php"
});
});
testApp.controller('testContr',function($scope){
//controller statements goes here
});
<ul class="nav nav-pills">
<li class="active" role="presentation"><a href="#/tab1">Tab 1</a></li>
<li role="presentation"><a href="#/tab2">Tab 2</a></li>
<li role="presentation"><a href="#/tab3">Tab 5</a></li>
<li role="presentation"><a href="#/tab4">Tab 4</a></li>
</ul>
tab1.php
<div>
tab1 content goes here
</div>
tab2.php
<div>
tab2 content goes here
</div>
tab3.php
<div>
tab3 content goes here
</div>
tab4.php
<div>
tab4 content goes here
</div>
第二條本辦法NG-顯示/ NG隱藏
main.php
var testApp = angular.module("testApp", []);
testApp.controller('testContr',function($scope){
$scope.view = 'tab1'// tab1 by default
$scope.setView = function($newView){
$scope.view = $newView;
}
//controller statements goes here
});
<ul class="nav nav-pills">
<li class="active" role="presentation" ng-click="setView('tab1')"><a href="#">Tab 1</a></li>
<li role="presentation" ng-click="setView('tab2')"><a href="#">Tab 2</a></li>
<li role="presentation" ng-click="setView('tab3')"><a href="#">Tab 3</a></li>
<li role="presentation" ng-click="setView('tab4')"><a href="#">Tab 4</a></li>
</ul>
<?php require_once 'tab1.php';
require_once 'tab2.php';
require_once 'tab3.php';
require_once 'tab4.php'; ?>
內容放在這些都在main.php但條件列出了NG-顯示
tab1.php
<div ng-show="view == 'tab1'">
tab1 content goes here
</div>
tab2.php
<div ng-show="view == 'tab2'">
tab2 content goes here
</div>
TAB3。 PHP
<div ng-show="view == 'tab3'">
tab3 content goes here
</div>
tab4.php
<div ng-show="view == 'tab4'">
tab4 content goes here
</div>
我看到局部視圖的好處與ng-route
,這是可管理的代碼塊。我可以實現php包括文件(每個文件具有獨立的視圖,並將它們全部包含在主文件中)和ng-show
。 我的應用不需要關心URL導航截至目前。 當我嘗試上述兩種方法我看到ng-show
更快,我能避免ng-route.js
文件以及(減小文件加載時間)。
有什麼我在想錯了。任何建議使用?
我忘了測試:)。這是另一個考慮因素,將來要維護你的代碼的開發者也不會責怪你。 :) – DilumN
嗨@nikjohn是的,我是來自jQuery的背景。這就是爲什麼我只是想顯示/隱藏的內容,因爲我們以前用jQuery做:)感謝測試建議我沒有去angularjs測試。正如我在截至目前的問題中所說的那樣,不需要URL導航。 +1我會遵循anuglar的做法。正如你所說,我會用ng-route。 – rram