3

Is there a best practice way of initializing an AngularJS app inside a non-AngularJS page? I'm adding a new feature to an existing webpage and need to pass a parameter in. Specifically, there is a set of tabs, and a new tab will launch an Angular app with the needed param from the current session.

Maybe build the parameter into the URL (ie URL param, etc)?

2 Answers 2

3

The first alternative

For intranet website I would recommend using Ajax GET request. This way you do not need to send data. It is already availible on the page. Here is an example with data from AngularJS tutorial (https://docs.angularjs.org/tutorial/step_05):

$http.get('phones/phones.html').success(function(data) {
    $("div.mainContent").html = data;
    $scope.phones = [
        {'name': 'Nexus',
         'snippet': 'Fast'}
    ];
});

The contents of phones.html:

<ul class="phones">
  <li ng-repeat="phone in phones | filter:query | orderBy:orderProp">
    <span>{{phone.name}}</span>
    <p>{{phone.snippet}}</p>
  </li>
</ul>

The second alternative

The second approach is to get a webpage with data populated in in a webpage from GET parameters.

<a href="/tab.html?name=Nexus&snippet=Fast">

C# Razor code:

<!DOCTYPE html>

<html>
<head>
    <title>Title</title>
</head>
<body>
  $scope.phones = [
    {'name': '@Request.Params["name"]',
     'snippet': '@Request.Params["snippet"]'}
  ];
  <div>
    <ul class="phones">
      <li ng-repeat="phone in phones | filter:query">
        {{phone.name}}
        <p>{{phone.snippet}}</p>
      </li>
    </ul> 
  </div>
</body>
</html>

All the methods you can choose from

You can use cookies, Get parameters, Post parameters, Web Storage (HTML5 feature), Websockets (HTML5 feature, e.g. SignalR) and using the data on the page if loading new page via AJAX. For Cookies, Get, Post, Web Storage you can use both HTML links and AJAX.

The choice between these four depends on the size of data you need to send and wheather you think about SEO. Get and cookies are not suitable for large amount of data. Get, AJAX, Websockets are bad for SEO. Cookies and Web Storage depend on the browser and may be deleted by user.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks, but this is for an intranet site. Helpful information for others though!
2

Yes, you can pass the params in through the URL and use a JS function to grab it out from within the Angular app:

function getParameterByName(name) {
    name = name.replace(/[\[]/, "\\\[").replace(/[\]]/, "\\\]");
    var regex = new RegExp("[\\?&]" + name + "=([^&#]*)"),
    results = regex.exec(location.href);
    return results == null ? "" : decodeURIComponent(results[1].replace(/\+/g, " "));
}

2 Comments

Is there a way to send a POST to the server from the non-Angular app and have it return a response that loads Angular? Maybe just some html with an ng-init embedded in it with the needed parameter?
Maybe, however keep in mind that Angular expects form data to be JSON. You could serialize it but I'd probably just go with the URL param.

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.