0

我使用$scope做了访问导航,我基于点击使用了它们,并且我使用了它们。我无法在变量容纳中设置网址。正确的URL是http://www.example.com/base/indexhttp://www.example.com/base/threadhttp://www.example.com/base/tag。 但结果始终是`http://www.example.com/base/index甚至认为,我点击另一个导航。

当我搜索我的问题后,答案是使$routeProvider$location。我可以在$ scope中使用$scope并设置URL吗?

这是我的代码:

$scope.states = {}; 
$scope.states.activeItem = 'nav1'; 

    $scope.items = [{ 
     id: 'nav1', 
     target: 'home', 
     title: 'Home', 
     icon: 'fa fa-home fa-3x' 
    }, { 
     id: 'nav2', 
     target: 'thread', 
     title: 'Thread + Answer', 
     icon: 'fa fa-briefcase fa-3x' 
    }, { 
     id: 'nav3', 
     target: 'tag', 
     title: 'Tag', 
     icon: 'fa fa-briefcase fa-3x' 
    }, 
    { 
     id: 'nav4', 
     target: 'trending_tag', 
     title: 'Trending Tag', 
     icon: 'fa fa-briefcase fa-3x' 
    }, 
    { 
     id: 'nav5', 
     target: 'category', 
     title: 'Category', 
     icon: 'fa fa-briefcase fa-3x' 
    }, 
    { 
     id: 'nav6', 
     target: 'user', 
     title: 'User', 
     icon: 'fa fa-user-circle-o fa-3x' 
    }]; 

    $scope.callToAction = function(actionName){ 

     if($scope[actionName]){ 
      $scope[actionName](); 
     }else{ 
      alert("YOUR PAGE NOT FOUND BECAUSE AKU BELUM BUAT PAGE NYA HEHE"); 
     } 
    }; 

顺便说一句,$scope.items执行我的导航。

+0

至于它似乎你想创建一个代码来显示菜单或东西有效的网址,对不对? – SPViradiya

+0

是的,你说得对。但我很困惑,如果使用这种方式设置网址。 – userpr

回答

1

您可以使用$location.url更改页面的URL。

$location.url('/yourUrl')将去/yourUrl。您可以将一个字符串作为变量传递给该函数。

请注意,您也可以访问到当前的URL与:

$scope.currentUrl = $location.url(); // <-- No params 

不要忘记inject$location在服务/控制器!

+0

我可以把$ location.url放在$ scope.items中吗?我的意思是这样的{ id:'nav1', $ location.url(); } – userpr

+1

这是一个导航栏?如果需要,你可以在'ng-click'中调用'$ location.url('nav1')''。 – Mistalis

+0

是的导航栏。但它不工作。 URL是正确的,但我的网页没有显示 – userpr

0

您可以使用$routeProvider进行导航。

angular.module('FunnyAnt.Examples.Routing') 
.config(function ($routeProvider) { 
    $routeProvider. 
    when('/home', { 
     templateUrl: 'embedded.home.html', 
     controller: 'HomeController' 
    }). 
    when('/about', { 
     templateUrl: 'embedded.about.html', 
     controller: 'AboutController' 
    }). 
    otherwise({ 
     redirectTo: '/home' 
    }); 
}); 

工作fiddle here