Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/464.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
从javascript链接到.cshtml视图_Javascript_Asp.net Mvc_Angularjs_Angularjs Directive - Fatal编程技术网

从javascript链接到.cshtml视图

从javascript链接到.cshtml视图,javascript,asp.net-mvc,angularjs,angularjs-directive,Javascript,Asp.net Mvc,Angularjs,Angularjs Directive,如何从javascript文件直接指向.cshtml视图? 例如,为什么我不能将.cshtml视图与angular.js一起使用? 如本例所示: .directive('encoder', ($timeout) => { return { restrict: 'E', transclude: true, scope: 'isolate', locals: { service:

如何从javascript文件直接指向.cshtml视图? 例如,为什么我不能将.cshtml视图与angular.js一起使用? 如本例所示:

 .directive('encoder', ($timeout) => {
        return {
            restrict: 'E',
            transclude: true,
            scope: 'isolate',
            locals: { service: 'bind' },
            templateUrl: 'encoderTemplate.cshtml' // <-- that's not possible?
        }
    });
.directive('encoder'),($timeout)=>{
返回{
限制:'E',
是的,
范围:'隔离',
局部变量:{service:'bind'},

templateUrl:'encoderTemplate.cshtml'/如评论中所述,您不能直接提供.cshtml文件,但是,如果您选择以下方式,您可以使用控制器呈现内容:

public class TemplateController : Controller
{
    // create a ~/Views/Template/Encoder.cshtml file
    public PartialViewResult Encoder()
    {
        return PartialView();
    }
}
然后像使用
@Url一样引用它。操作

{
    ....
    templateUrl: '@Url.Action("Encoder", "Template")'
}

评论

如果您的大部分JavaScript代码都位于具有Razor访问权限(例如external.js文件)之外,您仍然可以利用Url生成器,只需稍微改变一下即可。例如,我可能会执行以下操作:

public class TemplateController : Controller
{
    // Add a child method to the templates controller that outputs default
    // configuration settings (and, since it's a child action, we can re-use it)
    [ChildActionOnly]
    public PartialViewResult Index()
    {
        // You could build a dynamic IEnumerable<ConfigRef> model
        // here and pass it off, but I'm just going to stick with a static view
        return PartialView();
    }
}
另一个选择是:

1.使用EncoderTemplate视图添加TemplatesController

public class TemplatesController : Controller
{

     public ActionResult EncoderTemplate()
     {

           return View();
     }

}
2.将Layout=null添加到EncoderTemplate.schtml视图

@{
    Layout = null;
}

<div>Your html goes here</div>

不,因为IIS不会为.cSHTML文件提供服务,因为它不把它看作一个静态文件。您可能会考虑设置一个虚拟控制器来直接传递指定的.cSHTML文件。请您回答它。谢谢您,我不能使用@ URL动作,因为代码来自单独的JavaScript。js文件,并且没有嵌入到剃须刀中view@Agzam:您还可以将全局设置存储在站点根目录中,然后在子文件中引用。
var SiteSettings={templates:{encoder:'@Url.Action(“encoder”,“Template”)}
then
templateUrl:SiteSettings.templates.encoder
这正是我所做的,它很有魅力。但是到目前为止,我还没有想出如何在Visual Studio中使用Resharper作为运行程序与Jasmine进行单元测试。有什么想法吗?抱歉,我不知道。我没有使用Resharper。
public class TemplatesController : Controller
{

     public ActionResult EncoderTemplate()
     {

           return View();
     }

}
@{
    Layout = null;
}

<div>Your html goes here</div>
.directive('encoder', ($timeout) => {
    return {
        restrict: 'E',
        transclude: true,
        scope: 'isolate',
        locals: { service: 'bind' },
        templateUrl: '/Templates/EncoderTemplate' // you should not add .schtml
    }
});