Controller无法访问Module Angularjs MVC vs 2013

时间:2016-12-21 00:28:10

标签: angularjs model-view-controller module defined

我正在Error : Reference Error: myAppmd is not defined, when running the application

我在MVC项目的脚本文件夹中创建了app.js

var myAppmd = angular.module("myApp", []);

Controller“name = HomeController”文件存储在~/Scripts/angularScripts文件夹中。控制器代码在

之下
 myAppmd.controller("IndexCtrl",  function ()
    {
        $scope.students
        {
            LoginID,  LPassword;
        }

        $scope.submitForm = function($scope)
        {

        }
    }
    )

Bundle文件配置为:

bundles.Add(new ScriptBundle("~/bundles/angularz").IncludeDirectory(
"~/Scripts/angularScripts","*.js",true).Include("~/Scripts/app.js"));

_layout.cshtml

<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.5.6/angular.min.js"></script>
@Styles.Render("~/Content/css")
@Scripts.Render("~/bundles/angularz")
....

Index.cshtml文件代码在

之下
@{
    ViewBag.Title = "Index";
}

    <h2>Welcome to Students Screen/h2>
        <div>
        <form name="frmStudents" ng-app="myApp" ng-controller="IndexCtrl">
            <input name="LoginID" type="text" class="col col-lg-4" ng-model="LoginID" value="{{LoginID}}" required />
            <input name="LPassword" type="password" class="col col-lg-4" ng-model="LPassword" value="{{LPassword}}" required />

            <input type="submit" value="Login" ng-click="submitForm()" ng-disabled="$invalid" />
        </form>
    </div>

1 个答案:

答案 0 :(得分:0)

如果您的控制器位于单独的文件中,则需要在使用之前获取该文件中应用的引用。请注意,在应用中定义myAppmd并不会使其成为全局。

长话短说改变您的控制器代码:

myAppmd.controller("IndexCtrl",  function ()

要:

angular.module('myApp').controller("IndexCtrl",  function ()