提交表格时不调用控制器功能

时间:2014-10-26 17:42:18

标签: c# asp.net angularjs forms

当点击提交按钮时,没有调用auth功能,我无法找出原因。

所有页面模板的html:

<!DOCTYPE html>
<html ng-app ="myApp">
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>MyApp @ViewBag.Title</title>

<link href="~/Content/bootstrap.min.css" rel="stylesheet" type="text/css" />
<link href="~/Content/Site.css" rel="stylesheet" type="text/css" />
<script src="~/Scripts/modernizr-2.6.2.js"></script>
<script src="~/Scripts/jquery-1.10.2.min.js"></script>
<script src="~/Scripts/bootstrap.min.js"></script>
<script src="~/Scripts/angular.min.js"></script>
<script src="~/js/Login.js"></script>
</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("myApp", "Index", "Home", null, new { @class = "navbar-brand" })
        </div>
        <div class="navbar-collapse collapse">
            <ul class="nav navbar-nav">
            </ul>
        </div>
    </div>
</div>

<div class="container body-content">
    @RenderBody()
    <hr />
    <footer>
        <p>&copy; @DateTime.Now.Year - myApp</p>
    </footer>
</div>


</body>
</html>

此页面的html视图:

@{
    ViewBag.Title = "Login";
    Layout = "~/Views/Shared/_Layout.cshtml";
}

<h2></h2>
<div id="loginBox">
  <auth-modal></auth-modal>
</div>

这是自定义指令的html:

<div class="modal fade">
    <div class="modal-dialog">
        <div class="modal-content">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
            <h4 class="modal-title"></h4>
        </div>
        <div class="modal-body">
            <form class="form-signin" name="authForm" ng-submit="auth()" ng-controller ="AuthController" novalidate>
                <input type="text" class="form-control" placeholder="user name" ng-model ="AuthController.user.username" required autofocus>
                <input type="password" class="form-control" placeholder="password" ng-model ="AuthController.user.password" required>
                <input type="submit" class="btn btn-primary" value="Submit" />
            </form>
        </div>
    </div><!-- /.modal-content -->
</div><!-- /.modal-dialog -->

这是JS:

(function () {
var MyApp = angular.module('MyApp', []);

MyApp.controller('AuthModalController', function () {
    MyApp.user = {};
    console.log("AuthModalController ran");
    $(".modal").modal({});
});

MyApp.directive("authModal", function () {
    return { restrict: 'E', templateUrl: '\\js\\templates\\auth-modal.html', controller: 'AuthModalController',controllerAs: 'authmodalCtrl'}
});

MyApp.controller('AuthController',function () {
    this.user = {};
    console.log("AuthController ran");
    this.auth = function () {
        console.log("user " + this.user);

    };
});
})();

MVC 5控制器:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace MyApp.Controllers
{
    public class MyAppController : Controller
   {
       //
       // GET: /MyApp/
       public ActionResult Index()
       {
          return View();
       }
    }
}

1 个答案:

答案 0 :(得分:2)

编辑:我在这里添加了我无法获得下面链接的代码来运行,除非添加ng-app =&#34; app-name-from-module&#34;指令与表单对象上的控制器声明一起,它不在链接的angular-js文档中!

只阅读文档,我注意到了很多问题,

1)您正在声明&#34; myApp&#34;并使用&#34; MyApp&#34;除非是混淆错误。

2)我认为你的控制器每个文档遗漏了一些东西(esp $ scope variable,https://docs.angularjs.org/guide/controller

2a)您没有将auth功能附加到$ scope

3).controller似乎将字符串和数组作为参数,而不是泛型函数。

Per:https://docs.angularjs.org/api/ng/directive/ngSubmit

<script>
  angular.module('submitExample', [])
    .controller('ExampleController', ['$scope', function($scope) {
      $scope.list = [];
      $scope.text = 'hello';
      $scope.submit = function() {
        if ($scope.text) {
          $scope.list.push(this.text);
          $scope.text = '';
        }
      };
    }]);
</script>
<form ng-app="submitExample" ng-submit="submit()" ng-controller="ExampleController">
  Enter text and hit enter:
  <input type="text" ng-model="text" name="text" />
  <input type="submit" id="submit" value="Submit" />
  <pre>list={{list}}</pre>
</form>