【发布时间】:2016-03-09 20:08:39
【问题描述】:
我是 Angular 的新手,但我已经成功完成了几个基本示例。现在我正在考虑启动我的第一个真实世界的 Angular/MVC/WebAPI 应用程序。这将是一个时间卡类型的应用程序,我要做的第一件事是显示本地用户的当前时间。我在 Index.cshtml 页面上使用 Angular 做了一个基本的 HelloWorld,只是为了确保我对 Angular 的引用是正确的。那行得通....顺便说一句,我使用的是Angular v1.5。
我找到了以下帖子,我正在尝试混合使用代码。
http://www.codeproject.com/Articles/806029/Getting-started-with-AngularJS-and-ASP-NET-MVC-Par
http://stackoverflow.com/questions/23383233/how-to-make-a-ticking-clock-time-in-angularjs-and-html
但是我没有让它工作,它可能是我缺少的一些简单的东西,但我没有看到它。
这是我的 _Layout.csthml。我列出了 ng-app 和 ng-controller
<html>标签
<!DOCTYPE html>
<html ng-app="VirtualTimeClockApp" ng-controller="LandingPageController">
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>@ViewBag.Title</title>
@Styles.Render("~/Content/css")
</head>
<body>
<div class="navbar navbar-inverse navbar-fixed-top">
<div class="container">
<div class="navbar-header">
<button type="button" class="navbar-toggle" data-toggle="collapse" data-target=".navbar-collapse">
<span class="icon-bar"></span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
</button>
@Html.ActionLink("Application name", "Index", "Home", new { area = "" }, new { @class = "navbar-brand" })
</div>
<div class="navbar-collapse collapse">
<ul class="nav navbar-nav">
<li>@Html.ActionLink("Home", "Index", "Home")</li>
<li>@Html.ActionLink("About", "About", "Home")</li>
<li>@Html.ActionLink("Contact", "Contact", "Home")</li>
</ul>
@Html.Partial("_LoginPartial")
</div>
</div>
</div>
<div class="container body-content">
@RenderBody()
<hr />
<footer>
<p>© @DateTime.Now.Year - My ASP.NET Application</p>
</footer>
</div>
@Scripts.Render("~/bundles/bootstrap")
@Scripts.Render("~/bundles/angular")
@Scripts.Render("~/bundles/VirtualTimeClockApp")
@RenderSection("scripts", required: false)
</body>
</html>
这是我的主页/Index.cshtml
@model VirtualTimeClock.ViewModels.HomeViewModel
@{
ViewBag.Title = "Home Page";
}
<div class="jumbotron">
<h1>@Model.currentServerTime</h1>
<div ng-controller='TimeCtrl'>
<p>{{ clock | date:'HH:mm:ss'}}</p>
</div>
<p class="lead">ASP.NET is a free web framework for building great Web sites and Web applications using HTML, CSS and JavaScript.</p>
<p><a href="http://asp.net" class="btn btn-primary btn-lg">Learn more »</a></p>
</div>
这是我的 BundleConfig.cs 文件。最后一个 Bundles.Add 旁边是我捆绑控制器目录和主 .js 文件的位置。
namespace VirtualTimeClock
{
public class BundleConfig
{
// For more information on bundling, visit http://go.microsoft.com/fwlink/?LinkId=301862
public static void RegisterBundles(BundleCollection bundles)
{
bundles.Add(new ScriptBundle("~/bundles/bootstrap").Include(
"~/Scripts/bootstrap.js"));
bundles.Add(new ScriptBundle("~/bundles/angular").Include(
"~/Scripts/angular.js"));
bundles.Add(new ScriptBundle("~/bundles/VirtualTimeClockApp")
.IncludeDirectory("~/Scripts/Controllers", "*.js")
.Include("~/Scripts/VirtualTimeClock.js"));
bundles.Add(new StyleBundle("~/Content/css").Include(
"~/Content/bootstrap.css",
"~/Content/site.css"));
}
}
}
这是我的 VirtualTimeClock.js 文件。我在这里不是 100% 的事情是 VirtualTimeClockApp 的声明然后将一个模块添加到 ?
中引用的“VirtualTimeClockApp”var VirtualTimeClockApp = angular.module('VirtualTimeClockApp', []);
VirtualTimeClockApp.controller('LandingPageController', LandingPageController);
这是我的 LandingPageController.js 文件。
var module = angular.module('VirtualTimeClockApp', []);
module.controller('TimeCtrl', function ($scope, $interval) {
var tick = function () {
$scope.clock = Date.now();
}
tick();
$interval(tick, 1000);
});
【问题讨论】:
-
您是否在控制台中遇到任何错误,当您查看页面源代码时,请检查脚本被调用的顺序 - 它可能在
VirtualTimeClock.js之前调用LandingPageController.js -
@christiandev....我看不到任何错误。它只显示 {{ clock | date:'HH:mm:ss'}} 在页面中。所以有些东西没有正确地联系在一起。
-
您定义了
LandingPageController,但我没有看到您所指的功能。你的代码中应该有一个function LandingPageController(){};。 -
@KreepN ....再次,我仍在学习,但在 VirtualTimeClock.js 文件中它创建了一个名为“LandingPageController”的控制器,我假设下一个属性是 LandingPageController.js虽然它只是说 LandingPageController。然后在 LandingPageController.js 文件中有一个别名为 'TimeCtrl' 的函数将调用该函数将 $scope.clock 设置为当前日期。至少我是这么理解的。
-
@Caverman
I'm assuming the next property is the LandingPageController.js是一个错误的假设。第二个参数是你没有定义的函数名。 Angular 不会从这样的控制器中加载 JS 文件。
标签: angularjs asp.net-mvc