Transferring URL from web.config to an AngularJS .html view

In my current project, I am working on an application that utilizes Asp.net MVC along with Angularjs. One challenge I encountered is how to pass a URL, which is stored as a key in the web.config file, to a link within an Angularjs .html view.

For example, in the Web.config file, I have the following entry:

<add key="SampleTemplate" value="C:\SampleTemplate" />

Within the TemplateView.html file, I need to incorporate this URL into a link:

<a ng-href= "WHAT SHOULD I WRITE HERE?"> Edit Template</a>

Answer №1

One effective method is to access the config section on the server-side code and retrieve it using $http.get.

To achieve this, you can create a method in your MVC ApiController or a MVC Action. This method can look something like:

[Route("settings/{key}")]
[HttpGet]
public HttpResponseMessage GetSetting(string key)
{
    try 
    {
        return Request.CreateResponse(HttpStatusCode.OK,
            System.Configuration.ConfigurationManager.AppSettings[key]);
    }
    catch (Exception ex)
    {
        return Request.CreateErrorResponse(HttpStatusCode.BadRequest, ex)
    }
}

In your Angular service, you can then define a function that makes use of this method:

app.service('utilsService', utilsService);

utilsServcie.$inject = ['$http'];
function utilsServcie($http){
    this.readServerProperty = function(key){
        $http.get('/api/v1/settings/' + key).then(
            function(response){
                return response.data;
            },
            function(error){
                alert(error.data.message);
            }
        );
    }
}

You can utilize this service in specific areas by calling it like so:

app.controller('MainController', MainController);

MainController.$inject = ['utilsServcie'];
function MainController(utilsServcie){

    var vm = this;

    utilsServcie.readServerProperty('SampleTemplate').then(function(path){

        vm.path = path;

    });

}

Finally, in your view, you can display the information retrieved from the server:

<div ng-controller="MainController as mc">
    <a ng-href= "mc.path"> Edit Template</a>
</div>

This outline may contain minor errors since it was written from memory, but it provides a general idea of how to approach this solution.

That concludes the explanation! 😊

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Is AngularJS causing issues with Foundation modals? Any solutions to this problem?

Wondering how to manage Foundation4 modals with AngularJS? I've noticed that when I navigate from a modal to a new view, the old modal disappears but the page stays darkened as if it's still there in the background. I tried adding a class attribu ...

Associate the ng-model with ng-options value and label

I'm currently using ng-options to create a select tag that displays location options. The labels represent the location names, while the values indicate the corresponding location ID as stored in the database. In addition to binding the value (locati ...

Using Angular's ngBlur directive on multiple input fields

My current task involves capturing and saving all content on a webpage after it has been edited. For example, when a user clicks into an input field, enters data, and then clicks elsewhere, I would like to trigger a function to collect all the model data a ...

Exploring the Possibilities with AngularJS Location Filtering

My single-page application (SPA) includes select lists used for filtering locations based on region, state/province/EU country, and city. Although I have managed to set up filtering to some extent, there are issues with the lat/lng values of the locations ...

Skip a single test from a suite in Firefox using Protractor automation framework

I have a collection of tests in my tests folder, all named with the convention ending in spec.js. By using the */spec.js option in the Config file, I am able to run all tests seamlessly. However, I encountered an issue where I needed to skip running a spe ...

Ways to verify if a value is a numeric value

Here is a function that I am working with: $scope.SearchTicketEvent = function (ticketPinOrEvent) { if (ticketPinOrEvent != undefined) { if (ticketPinOrEvent.length == 10) ...

Bidirectional communication within a checkbox collection using AngularJS

Here, I am retrieving a list of objects to create a list of checkboxes based on the value of the IsActive column. The checkbox is either checked or unchecked depending on this boolean value. <div class="col-xs-12"> <div class="col-xs-12" ng-r ...

Unit Testing in Angular with spyOn().and.callThrough() does not execute the actual function

I am new to unit testing using Jasmine in Angular and currently exploring how to test a service with the function loadSomething(id) and added a console.info statement in it. My Service Code: function loadSomething(id) { console.info('this is a t ...

Angular Components unexpectedly lose background transparency when using Transparent-Background-PNG in the foreground

Hey there! I'm currently working on a landing page and I have this amazing idea to use a stunning picture of clouds with a transparent background layered over a beautiful landscape. The only problem is that when I try to implement it, the transparency ...

Apply a class to the input element within an ng input tag

When working with ng-input-tag, I tried to apply a bootstrap class to the input element of HTML in order to utilize specific bootstrap styles. Unfortunately, it did not work as expected. Below you can find the code snippet: angular .module('m ...

Animating nested ng-repeat loops

Trying to animate a list of elements divided by rows (the parent ng-repeat) and columns (the child ng-repeat) has been quite challenging. While I was able to achieve the desired animation with individual ng-repeats, the same cannot be said for nested ng- ...

ng-repeat did not properly listen for changes in the radio box selection

Feeling a bit confused here. I'm trying to call a function on change and pass the obj to it. From what I understand, since it's bound to the selected obj, I should be able to just use ng-model. However, in this situation, nothing happens when I s ...

Oops! An error occurred in AngularJs: "TypeError: $scope.todos.push is not a

I am currently facing an issue while using the $http.get method to add a todo from my controller to my database. The error message "TypeError: $scope.todos.push is not a function" keeps appearing, despite trying various solutions suggested by similar quest ...

Angularjs - How come I am able to modify an object but not the list (ng-repeat) from a separate view?

After updating the customers object in the console, I noticed that the list (ng-repeat) is not reflecting the changes. What should I do? Interestingly, it works fine when I implement this function and view2.htm's HTML inside page.htm. HTML "page.htm" ...

What is the best way to locate the nearest marker using the Google Maps Direction Service?

Currently, I am engaged in the development of a Google Maps project where I am integrating markers retrieved from a database onto the map using the drawMarkers function. In addition to this, the Google Maps feature tracks your current location and refreshe ...

One way to choose an element based on its class name with jqLite

I am currently working on optimizing my Angular.js app by removing jQuery and replacing it with Angular's jqLite in order to reduce the app's weight. However, I have run into a challenge as my app heavily relies on using find('#id') and ...

I am interested in utilizing node.js to input data into a mongodb Database

Currently, I am working on writing data to a JSON file and retrieving it back to an HTML page for display. Now, I want to achieve the same functionality with a MongoDB database. I have made some attempts, but unfortunately, it is not functioning as expecte ...

Ways to enhance filtering capabilities in AngularJS?

Currently, I am using the following ng-model: <input ng-model="query" type="text" iplaceholder="Add New Filter"></input> This is how I am presenting my data: <ul> <li role="menuitem" ng-repeat="item in availableFilters | filter:qu ...

AngularJS Kendo UI: Elevating Your Web Development Experience

This is my first time working on an Angular Js application as part of a project requirement. I have been searching online for documentation on installing Kendo for Angular, but most resources seem to focus on Angular instead of Angular Js. Is it possible ...

Utilize the ng-click feature for swiping interactions in Ionic v1

I have a slide page on Ionic V1 that utilizes previous and next buttons. Here is an example: <button id="" class="button button-slide prev no-animation" ng-click="prev()" ng-show="activeIndex > 0" > BACK </button> While the click function ...