5

I'm creating an angularJs application in MVC 5. I've created a directive as given below.

app.directive('clusterButton', function () {
    return {
        restrict: 'E',
        scope: {
            clusterInfo: '=info'
        },
        templateUrl: function(elem, attr){
            return 'ClusterButtonTemplate';
        }
    };
});

I have .cshtml file named ClusterButtonTemplate.cshtml and I created a partial view method in the controller class to return this view. So the angular js directive is binding the template.

I need to remove the method in mvc controller class and need to refer the template file directly. I don't want to use a cshtml file. I need a html file for template. I have created a ClusterButtonTemplate.html file. But while setting the template as below the browser shows a 404 error.

app.directive('clusterButton', function () {
    return {
        restrict: 'E',
        scope: {
            clusterInfo: '=info'
        },
        templateUrl: function(elem, attr){
            return 'ClusterButtonTemplate.html';
        }
    };
});

I didn't use angular js routing in my project. Everything is managed using the MVC routing. How can I fix this issue. Do I need to use Angular routing and MVC routing?

1
  • 2
    Can you directly load the html file by putting the path on the URL? Also you may use fiddler and check what is the actual path being loaded for the html file. Commented Dec 14, 2015 at 6:55

3 Answers 3

4

You'll need to put in in a place where MVC will not try to map the URL to a controller. By default, the /Content folder is ignored, so you could create a folder under it called "Templates" and use that for the URL.

templateUrl: function(elem, attr){
    return '/Content/Templates/ClusterButtonTemplate.html';
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks, saved me after trying a hundred other options given in other threads. Was close to making a new question for this :)
0

You may try to use a leading slash since it's loaded using a relative path.

        templateUrl: function(elem, attr){
            return '/ClusterButtonTemplate.html';
    }

2 Comments

Thank you for your reply. It is not worked. I'm getting a 404 not found exception. The file is located in Views/Home/ClusterButtonTemplate.html. I tried all possible paths. But issue not fixed. I think the issue is due to Angular JS and MVC routing conflict. But I don't know how to fix this.
Ahh. So that's why it is not working since it's located in Views/Home location. Change your url to '/Views/Home/ClusterButtonTemplate.html
0

That's very simple! Just put an html file template (as content) in the folder that you prefer (ie. app/phone-list/ in project root) then assign

templateUrl: '/app/phone-list/phone-list.template.html'

to controller templateUrl property. Avoid the views, model or controller folders...

Comments

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.