ale*_*dru 18 javascript angularjs
嗨,我是一个相当新的角度,我刚刚开始我的第一个应用程序.这是我到目前为止所做的.这是我的索引文件:
<body ng-app="codeArt">
<div ng-controller="loginCtrl">
<ng-include src="/App/scripts/login/login.html"></ng-include>
</div>
<!-------------- Angular Libs ---------------------->
<script src="/App/libs/angular/angular.js"></script>
<!--------------- Code Art Main Module -------------->
<script src="/App/scripts/codeArt.js"></script>
<!--------------- Code Art Login Module -------------->
<script src="/App/scripts/login/login.js"></script>
</body>
Run Code Online (Sandbox Code Playgroud)
索引文件存储在Views/Home/Index.cshtml中.这将是此处存储的唯一文件,因为我使用了ASP.NET MVC,所以需要它.
我在App/scripts/codeArt.js中定义了我的主模块:
var codeArt = angular.module("codeArt", [
'codeArt.login'
]);
Run Code Online (Sandbox Code Playgroud)
然后,我创建了一个aditional模块,其中包含与登录和注册App/scripts/login/login.js相关的功能
var login = angular.module('codeArt.login', [])
Run Code Online (Sandbox Code Playgroud)
我在App/scripts/login/controllers/loginCtrl.js中定义了一个控制器:
login.controller('loginCtrl', function($scope){
$scope.name = "Nistor Alexandru";
$scope.clicked = function(){
alert("I was clicked");
}
});
Run Code Online (Sandbox Code Playgroud)
我还在App/scripts/login/login.html中定义了登录页面的主视图,它看起来像这样:
<section>
<div>{{name}}</div>
<button ng-click="clicked()">Clicked Alert!</button>
</section>
Run Code Online (Sandbox Code Playgroud)
从我的index.cshtml文件中可以看出,我尝试使用ng-include来加载login.html文件.出于某种原因,当我运行应用程序时,我只能找到一个注释,其中包含ng-include:undefined和html没有加载.
我会在某些时候集成路由,但现在我想了解为什么这不能正常工作?
我也试过这个路径../../App/scripts/login/login.html因为它会为与index.cshtml文件相关的文件提供帮助,但事实并非如此.
luc*_*uma 34
ng-include应该是这样的,你错过了一个引用:
<ng-include src="'/Content/App/scripts/login/login.html'"></ng-include>
Run Code Online (Sandbox Code Playgroud)
角度表达式评估为URL.如果源是字符串常量,请确保将其包装在单引号中,例如src ="'myPartialTemplate.html'".
https://docs.angularjs.org/api/ng/directive/ngInclude
**旧答案**
我建议你将静态html文件放在content文件夹中,或者创建一些路由来从控制器返回它们.它应该在内容文件夹中开箱即用:
<ng-include src="/Content/App/scripts/login/login.html"></ng-include>
Run Code Online (Sandbox Code Playgroud)
或者您可以考虑创建一个TemplateController,然后您可以根据您请求的文件的名称返回视图(不要忘记设置路由):
<ng-include src="'/Template/get/whatever'"></ng-include>
public ActionResult Get(string name )
{
return View(name); // return View(name + ".cshtml")
}
Run Code Online (Sandbox Code Playgroud)
这假设您的文件夹里面/Views/Template/有名称whatever.cshtml.我认为使用控制器的好处是它们通过您的控制器代理(您可以稍后修改文件夹结构并轻松更新控制器中的引用)并可能传递一些服务器端数据,但这取决于您的要求.
| 归档时间: |
|
| 查看次数: |
13923 次 |
| 最近记录: |