Angular controller is not being called

时间:2016-08-31 12:16:58

标签: angularjs asp.net-mvc

I am new to Angular Js. I want to get data from MVC controller and display in MVC view through angular js. My AngularJs controller is:

  angular.module('VC', []).controller('MenuController', ['$scope', '$http', function ($scope, $http) {
    console.log("ANGULAR JS");
    $scope.getManufacturerList = function () {

        $http.get('/Home/GetManufacturerList').success(function (response) {
            console.log('success - manufacturer')
            console.log(response);
        })
        .error(function (data) {
            console.log(data);
        });
    }

}]);

In view, I am calling angular JS controller as

<nav class="navbar navbar-default navbar_btm" ng-controller="MenuController" ng-init="getManufacturerList()">

in MVC view, the action is

public ActionResult GetManufacturerList()
{
    var manufacturer = _vcService.GetManufacturer();
    return Json(manufacturer, JsonRequestBehavior.AllowGet);
}

angular js controller is not even initiating. Any one let me know what I've done wrong in this code?

2 个答案:

答案 0 :(得分:4)

You missed adding ng-app.

<nav ng-app="vc" class="navbar navbar-default navbar_btm" ng-controller="MenuController" ng-init="getManufacturerList()">

答案 1 :(得分:0)

You need to use ng-app directive here.

Refer the below example:

<html>
   <head>
      <title>Hello Angular</title>
      <link rel="stylesheet" type="text/css" href="http://maxcdn.bootstrapcdn.com/bootstrap/3.2.0/css/bootstrap.min.css">
   </head>
   <body>
      <div ng-app="HelloApp" ng-controller="HelloCtrl">
        <h1>Hello, {{name}}</h1>
        <form>
           <input type="text" ng-model="name" name="name">
        </form>  
      </div>
      <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/angularjs/1.3.3/angular.min.js">           </script>
      <script type="text/javascript">
           angular.module('HelloApp', [])
              .controller('HelloCtrl', ['$scope', function($scope){
                  $scope.name = "Calvin";
                }])
      </script>
   </body>
</html>