0

I have made access navigation with $scope and I used them based on click and I did. I can't set URL in variable accommodate. The correct URL is http://www.example.com/base/index or http://www.example.com/base/thread, or http://www.example.com/base/tag. But the result is always `http://www.example.com/base/index even thought , I clicked another navigation.

After I search my problem, the answer is make $routeProvider and $location. Can I use $scope and setting URL inside $scope?

This is my code:

$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");
        }
    };

By the way, $scope.items to execute my navigation.

2
  • As far as it seems you wanted to created a code to show the active URL in the menu or something, right? Commented Feb 22, 2017 at 7:54
  • Yes, you right. But im confused to setting url if using that way. Commented Feb 22, 2017 at 8:03

2 Answers 2

1

You can change the URL of your page by using $location.url.

$location.url('/yourUrl') will go to /yourUrl. You can pass a String as a variable to the function.

Note that you can also access to the current URL with:

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

Don't forget to inject $location in your service/controller!

4
  • Can I put $location.url in $scope.items ? I mean like that { id: 'nav1', $location.url(); } Commented Feb 22, 2017 at 7:54
  • 1
    Is that for a navbar? You can call $location.url('nav1') in a ng-click if needed. Commented Feb 22, 2017 at 7:55
  • Yes for navbar. But its not working. URL is correct but my page not showing Commented Feb 22, 2017 at 7:59
  • Im not using routeProvider. Commented Feb 22, 2017 at 8:09
0

You can use $routeProvider for navigation.

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'
    });
});

working fiddle here

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.