【问题标题】:When submitting form not calling controller function提交表单时不调用控制器功能
【发布时间】:2014-10-26 17:42:17
【问题描述】:

单击提交按钮时,未调用 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();
       }
    }
}

【问题讨论】:

  • 需要您的控制器代码。
  • @CBauer 这是我发布的最后一个代码块
  • 我在看,你有MyApp.controller('AuthController',['$scope',function () {,你需要MyApp.controller('AuthController',['$scope',function (**$scope**) {
  • 尝试从缩小的角度切换到非缩小的角度,异常细节会更清晰,我们也许可以为您提供帮助(但老实说,我们已经偏离了原来的帖子,这应该是一个新问题)。

标签: c# asp.net angularjs forms


【解决方案1】:

编辑:我在这里补充说,我无法运行下面链接的代码,除非在表单对象上的控制器声明旁边添加 ng-app="app-name-from-module" 指令,这不在链接的 angular-js 文档中!

仅阅读文档我发现了很多问题,

1) 除非那是混淆错误,否则您声明“myApp”并使用“MyApp”。

2) 我认为您的控制器在文档中遗漏了一些东西(尤其是 $scope 变量,https://docs.angularjs.org/guide/controller

2a) 你没有将 auth 函数附加到 $scope

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

每个人: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>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-09-07
    • 1970-01-01
    • 2020-06-21
    • 1970-01-01
    • 2020-03-05
    • 1970-01-01
    • 2021-01-30
    • 2019-03-08
    相关资源
    最近更新 更多