0

This is my code so far, to return a view in browser via Angular custom directive, using @html.partialview

var newOne = function () {
    return {
        restrict: "A",
        templateUrl: "newone.html",
        replace: true,
        scope: {},
        controller: ['$scope', function ($scope) {
            }]
    }
}

I declare the id "newone.html" here:

<script type="text/ng-template" id="newone.html">
            @Html.Partial("~/Views/AngularTemplates/Newone.cshtml")
        </script>

and ofcourse I call the directive from the html:

<div data-new-one></div>

Unfortunately I do not get the appropriate response. All I get is an error message(console inspector): [$compile:tpload] Failed to load template: newone.html

I tried to store the .cshtml in multiple folders, or even load it from bundleconfig... But nothing! Any ideas? Thank you!

3
  • Put that template in a HTML file or use the templateCache module. Commented May 4, 2016 at 19:12
  • First, it sounds like @html.partial is an ASP.NET command so you should add that as a tag in your question. Also, you should check the developer console and ensure that the <script> with the HTML is actually being loaded by ASP.NET. If it's blank or if ASP.NET is giving an error message, that's your problem. Commented May 4, 2016 at 19:12
  • Hi, try using the parameter template instead. Commented May 4, 2016 at 19:13

4 Answers 4

1

You shouldn't need to use the @Html.Partial.

Try changing your directive code to this:

var newOne = function () {
    return {
        restrict: "A",
        templateUrl: "~/Views/AngularTemplates/Newone.cshtml",
        replace: true,
        scope: {},
        controller: ['$scope', function ($scope) {
            }]
    }
}
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you but unfortunately this didn't work. I don't get the error any more but neither the appropriate view returns as well.
0

Create a new file name named:

~/Views/AngularTemplates/Newone.**tpl**.cshtml

Just copy and past contents of old file into new tpl.html file adding tpl(template) before the cshtml or html

Example:

stuff.tpl.html

Comments

0

Found the solution in Angular documentation:
https://docs.angularjs.org/error/$compile/tplrt which says:

the template must have exactly one root element

Seems that multiple <div> tags or even comments are getting the error.

Comments

0

You can import template in the script and use it as a "template", not "templateUrl":

import newone from 'newone.html';
var newOne = function () {
return {
    restrict: "A",
    template: newone,
    replace: true,
    scope: {},
    controller: ['$scope', function ($scope) {
        }]
      }
    }

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.