Sam
Sam

Reputation: 5657

Why is ng-click not firing on this button?

The ng-click button is not working.

Does anyone see anything wrong with the following snippet?

var locationModel = {
  "City": "Alexandria",
  "State": "State"
};

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

locationApp.controller("locationCtrl", function($scope) {
  $scope.newLocation = locationModel;

  $scope.addLocation = function(state) {
    console.log(state);
    //$http.post("/Home/CreateLocation", newLocation).then(function (response) {
    //    console.log("Inserted", data);
    //});
  }
});
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.6.1/angular.min.js"></script>

<body ng-controller="locationCtrl">
  <div>

    <button class="btn btn-default" ng-click="addLocation('VA')">
      Add
    </button>

  </div>
</body>

Upvotes: 0

Views: 1902

Answers (3)

Sam
Sam

Reputation: 5657

Yes - I had forgot the ng-app tag. I meant to put it in the html tag. It is working now.

Upvotes: 0

You need to add the ng-app directive so your app runs, update the body tag to become as following:

<body ng-controller="locationCtrl" ng-app="locationApp">

Upvotes: 1

Yaser
Yaser

Reputation: 5719

You need ng-app somewhere in your html:

var locationModel = {
        "City": "Alexandria",
        "State": "State"
    };

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

    locationApp.controller("locationCtrl", function ($scope) {
        $scope.newLocation = locationModel;

        $scope.addLocation = function (state) {
            console.log(state);
            //$http.post("/Home/CreateLocation", newLocation).then(function (response) {
            //    console.log("Inserted", data);
            //});
        }
    });
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>

<body ng-app="locationApp" ng-controller="locationCtrl">
<div> 

    <button class="btn btn-default" ng-click="addLocation('VA')">
        Add
    </button>

</div>
</body>

Upvotes: 1

Related Questions