2017-01-05 52 views
0

ng-click按钮不起作用。为什么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”,因为它不在体上? @Sam – Yaser

回答

1

您需要ng-app在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>

1

您需要添加ng-app指令,以便您的应用程序运行,更新body标签变得像佛陀一样llowing:

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

0

是 - 我已经忘记了NG-app标记。我的意思是把它放在html标签中。 现在正在工作。

相关问题