3

I'm searching for an alternative that is more "angular-ish" than my current solution:

I have defined in my app.js, amongst others, the following routes:

app.config(function ($routeProvider) {
    $routeProvider
      .when('/Table/:guid', {
        templateUrl: 'views/table.html',
        controller: 'TableCtrl',
        controllerAs: 't'

      })
      .when('/Table/:guid/new', {
        templateUrl: 'views/addPost.html',
        controller: 'AddPostCtrl',
        controllerAs: 'ap'
      })
      .otherwise({
        redirectTo: '/'
      });
});

And in my controller:

angular.module('acpApp')
.controller('TableCtrl', function ($scope, $location, $routeParams) {
    var param = $routeParams.guid;      
    $scope.currentUrl = $location.absUrl();
}

And in my view:

<a ng-href="{{currentUrl}}/new" class="btn btn-primary"><span class="glyphicon glyphicon-plus" aria-hidden="true"></span> Add post</a>

This works, but I don't feel like it is the angular way to do it - or is it? How can I anguliarize this?

Zyberzero
  • 1,604
  • 2
  • 15
  • 35
  • Probably `$scope.currentUrl = $location.absUrl() + '/new';` and `ng-href="{{currentUrl}}"`? – Joy Aug 28 '15 at 15:21

1 Answers1

0

the way i would make this is in controller:

angular.module('acpApp')
   .controller('TableCtrl', function ($scope, $location, $routeParams) {
      $scope.guid = $routeParams.guid;
   }

and the view:

<a ng-href="/Table/{{guid}}/new" class="btn btn-primary"><span class="glyphicon glyphicon-plus" aria-hidden="true"></span> Add post</a>
  • Yeah, that was my initial thought as well. But then I actually need to hardcode the route into the view, which I think I would like to avoid for some reason... – Zyberzero Aug 28 '15 at 15:13
  • in case that you need to fix the route /Table you could add into a $scope variable too and print it into the route. Because you only fix this once time – AlexAnderz SweetGuitar Aug 28 '15 at 15:33