2017-01-05 1 views
0

ng-click 버튼이 작동하지 않습니다.왜이 버튼에 클릭이 발생하지 않습니까?

다음 스 니펫에 문제가있는 사람이 있습니까?

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>

+0

body 태그의 부모에'ng-app = "locationApp"속성이 있습니까? 앱이 없으면 앱이 실행되지 않습니다. –

+0

본문에 없으므로 HTML 태그에 ng-app = "locationApp"이 있습니까? @ 샘 – Yaser

답변

1

당신은 어딘가에 HTML로 ng-app 필요합니다

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>

1

당신은 body 태그를 업데이트하므로 응용 프로그램 실행을 ng-app 지시문을 추가 할 필요가 fo로 변하기 llowing는 :

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

0

예 - 나는 NG-응용 프로그램 태그를 잊어 버렸습니다. 나는 HTML 태그에 넣으려고했다. 지금 작동 중입니다.

관련 문제