0

Hello I have SPA application with angularjs and I dont know how to create component with template url.

My directory structure is:

SpaApp
 - Scripts
  - App
   - test.component.js
 - Controllers
 - Views
  - Home
   - test.component.cshtml

test.component.js

app.component("appTest", {
    templateUrl: 'test.component.cshtml', //what is the address here?
    transclude: true,
    controller: function AppTest() {}
})

I tried add url to Views/Home/test.component.cshtml and it was not working. Then I tried add html template to same folder as is the component javascript and it is not working too. Where is my problem? What is the address to html template for component? Thanks you

1 Answer 1

1

You can't use razor views directly as angular templates. Razor views are compiled by ASP.NET and not served directly as static files by the web server, while Angular templates are either loaded asynchronously over http or retrieved through angulars templatecache.

You have two options.

  1. Don't use razor views (i.e. .cshtml-files) as angular templates. Use regular .html-files instead.
  2. Reference the ASP.NET MVC action, instead of a static file. In your case (assuming default routing configurations) your templateUrl should be /Home/Test instead.

I recommend the first option and keep ASP.NET MVC razor views completely separate from angular.

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

2 Comments

Thank you, I tried your first option. I had html file in the same folder as js file but it is not working too. Must I have this html file path in bundle config?
The path to the html template must be the relative path, from the root of your application, where the root is the running directory of your ASP.NET application.

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.