0
AngularJS noob here。AngularJS:我如何在URL中獲得散列值
我希望我的應用程序能夠響應網址哈希。通過迴應,我的意思是設置一個變量值等簡單的東西。
在這個例子中,如果該URL爲HTTP:example.com/#parents-to-be,我希望應用程序執行的標準行爲是滾動到#父母將要,並且還設置了$ scope.tab3 = 1;
從做一些研究,似乎這樣才能與$的location.hash()和$ anchorScroll()的組合來實現。但是,迄今爲止我還沒有取得成功。有人可以幫忙嗎?
HTML:
<div ng-app="bug">
<div ng-controller="TabsController">
<a href ng-click="activateTab(1)">tab 1</a>
<a href ng-click="activateTab(2)">tab 2</a>
<a href ng-click="activateTab(3)">tab 3</a>
<div id="parents-to-be"> <!-- this is the target div -->
<div ng-show="tab1 === 1">content of tab 1</div>
<div ng-show="tab2 === 1">content of tab 2</div>
<div ng-show="tab3 === 1">content of tab 3</div>
</div>
</div> <!-- /angular tabs controller scope -->
</div> <!-- /angular app scope -->
app.js:
(function() {
var app = angular.module('bug', []);
app.controller('TabsController', function($scope, $location, $anchorScroll) {
// init tabs
$scope.initTabs = function() {
$scope.tab1 = 0;
$scope.tab2 = 0;
$scope.tab3 = 0;
};
// activate tabs
$scope.activateTab = function(tab_index) {
$scope.initTabs();
$scope['tab' + tab_index] = 1;
};
// this is the part of the app where I try to get the hash value and edit the value of tab3
$scope.hash_value = $location.hash();
if ($scope.hash_value === "parents-to-be") {
$scope.activateTab(3);
$anchorScroll();
}
});
})();
謝謝!
使用'$ location.search()' – Amy 2014-10-09 07:42:36