Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • Paul Marden 235 posts 338 karma points MVP 2x c-trib
    Feb 19, 2016 @ 16:38
    Paul Marden
    0

    Retrieve Hostname in Angular for Property Editor

    I'm trying to create a property editor that will retrieve the full URL of the current page being edited in the backend.

    I've managed to access part of the Url but it's missing the hostname.

    angular.module("umbraco")
        .controller("MyPropertyEditor.controller",
        function ($scope, editorState) {
            $scope.url = editorState.current.urls[0];
        }
    );
    

    Any ideas how I can get the full url including protocol and hostname in Angular?

  • Tom Madden 253 posts 455 karma points MVP 4x c-trib
    Feb 19, 2016 @ 16:58
    Tom Madden
    1

    would you not use $location.absUrl(); see https://docs.angularjs.org/api/ng/service/$location

    t

  • Paul Marden 235 posts 338 karma points MVP 2x c-trib
    Feb 23, 2016 @ 12:10
    Paul Marden
    0

    Thanks Tom, but that just gives me access to the current browser location. The issue is that my Umbraco instance has multiple root nodes mapped to different domains. The domain used to log into Umbraco is not necessarily the domain on the node I'm trying edit in the Property Editor.

  • Warren Buckley 2106 posts 4836 karma points MVP 7x admin c-trib
    Feb 23, 2016 @ 12:25
    Warren Buckley
    0

    Hello Paul,
    I don't think Umbraco ships with any AngularJS service that would let you know what Hostnames/Domains is available.

    I would recommend creating a C# Web API controller that your AngularJS can do a $http.get() to and return some JSON of the hostnames that this node has available.

    You would need to create an UmbracoAuthorisedApiController to ensure its not publicly available and only available in the backoffice of Umbraco and then simply use the Umbraco APIs to fetch the domain/hosts of that node id.

    Hope that gives you a starting point or an idea on how I would go about tackling the problem.

    Cheers,
    Warren

  • Comment author was deleted

    Feb 23, 2016 @ 12:16

    You should be able to fetch the current node by injecting the editor state http://umbraco.github.io/Belle/#/api/umbraco.services.editorState, it might be that it also exposes the url but not 100% (if not you'll need to fetch the url based on the id)

  • Comment author was deleted

    Feb 23, 2016 @ 12:20

    If the editorstate doesn't include the url you can fetch it with the ContentResource ( getNiceUrl(id) ) http://umbraco.github.io/Belle/#/api/umbraco.resources.contentResource

  • Comment author was deleted

    Feb 23, 2016 @ 12:21

    For the hostname you'll probably need to write some code since I don't see a hostnameResource or Service in the docs

  • Comment author was deleted

    Feb 23, 2016 @ 12:23
  • Paul Marden 235 posts 338 karma points MVP 2x c-trib
    Feb 23, 2016 @ 12:35
    Paul Marden
    0

    Cheers guys I'll use that as a starting point and let you know how I get on.

    Thanks

  • Paul Marden 235 posts 338 karma points MVP 2x c-trib
    Feb 24, 2016 @ 11:20
    Paul Marden
    100

    A bit of a facepalm moment, but I found that I didn't have any hostnames in my test umbraco instance! So.... that's why editorState.current.urls didn't have any host names.

    So now I can get at the urls in the controller without any server side code.

    There is one exception. If the current url that umbraco is being accessed is the same as one of the hostnames attached to a node, then the hostname is not returned by editorState.current.urls.

    e.g. if you bind mysite.local to a node and have a Property Editor on that node, then editorState.current.urls will return /node-name not mysite.local/node-name.

    That's a simple one to fix though using $location rather than using the server side code.

Please Sign in or register to post replies

Write your reply to:

Draft