【问题标题】:Angular function not working?角功能不起作用?
【发布时间】: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>&copy; @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 &raquo;</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


【解决方案1】:

将您的 VirtualTimeClock.js 更改为...

(function () {
    'use strict';
    angular.module('VirtualTimeClockApp', []);
})();

(function () {
    'use strict';

    angular
        .module('VirtualTimeClockApp')
        .controller('LandingPageController', LandingPageController);


    function LandingPageController() {

    }
})();

和另一个文件:

(function () {
    'use strict';

    angular
        .module('VirtualTimeClockApp')
        .controller('TimeCtrl', TimeCtrl);

    TimeCtrl.$inject = ['$scope', '$interval'];

    function TimeCtrl($scope, $interval) {
        var tick = function () {
            $scope.clock = Date.now();
        }
        tick();
        $interval(tick, 1000);
    }
})();

并重新排序捆绑包...

 bundles.Add(new ScriptBundle("~/bundles/VirtualTimeClockApp")
                    .Include("~/Scripts/VirtualTimeClock.js")
                    .IncludeDirectory("~/Scripts/Controllers", "*.js")
                    );

我认为主要问题是捆绑订单,但使用IIFE 更简洁。另外,值得一读style guide

【讨论】:

  • 宾果游戏!有一点变化。我最初使用了您的代码,但它不起作用(即使我认为它应该是)。然后我决定让控制器在页面的特定部分工作,而不是在我要构建的每个页面中工作。因此,我将 ng-controller="LandingPageController" 从 _Layout.cshtml 页面中的 HTML 标记移到了
    ,这是我希望这个 angular 工作的地方。然后它开始工作。谢谢您的帮助!我相信很快会有其他问题。
  • @Caverman,将 dom 中的位置移到较低位置应该没有任何区别 - 我认为这是捆绑。无论哪种方式,请查看该样式指南,它在开始时对我很有帮助。祝你好运。
【解决方案2】:

从这里删除括号

var VirtualTimeClockApp = angular.module('VirtualTimeClockApp',[]);

并创建一个名为 LandingPageController 的函数。它应该是这样的:

var VirtualTimeClockApp = angular.module('VirtualTimeClockApp');

VirtualTimeClockApp.controller('LandingPageController',   LandingPageController);

function LandingPageController() {}

如果您使用方括号,您将创建一个新模块,在这种情况下,该模块已经在您的 LandingPageController.js 文件中创建


【讨论】:

  • @Sergio....感谢您的建议。我删除了括号,但它仍然无法正常工作。我会把括号去掉。我从上面的链接之一中提取了示例代码。我现在有“var module = angular.module('VirtualTimeClockApp');”在我的 LandingPageController.js 文件中。
  • @Caverman 我更新了答案。我之前对这些名字感到困惑。有两个问题,没有定义 LandingPageController 函数,还有括号;)
猜你喜欢
相关资源
最近更新 更多
热门标签