如何恢复在angularJS中单击的变量

时间:2017-03-17 23:28:31

标签: javascript angularjs

只需要恢复(在' myfunction')我的变量' name'当我点击我的按钮时,在我的行上

这是我的索引页

var app = angular.module('myApp', []);
app.controller('customersCtrl', function($scope, $http) {
  $http.get("customers.php").then(function (response) {
      $scope.myData = response.data.records;
      console.log($scope.myData);
      $scope.myfunction = function(){


      }
  });
});

这是我的控制器

{ "records":[ {"Name":"Alfreds Futterkiste","City":"Berlin","Country":"Germany"}, {"Name":"Ana Trujillo Emparedados y helados","City":"México D.F.","Country":"Mexico"}, {"Name":"Antonio Moreno Taquería","City":"México D.F.","Country":"Mexico"}, {"Name":"Around the Horn","City":"London","Country":"UK"}, {"Name":"B's Beverages","City":"London","Country":"UK"}, {"Name":"Berglunds snabbköp","City":"Luleå","Country":"Sweden"}, {"Name":"Blauer See Delikatessen","City":"Mannheim","Country":"Germany"}, {"Name":"Blondel père et fils","City":"Strasbourg","Country":"France"}, {"Name":"Bólido Comidas preparadas","City":"Madrid","Country":"Spain"}, {"Name":"Bon app'","City":"Marseille","Country":"France"}, {"Name":"Bottom-Dollar Marketse","City":"Tsawassen","Country":"Canada"}, {"Name":"Cactus Comidas para llevar","City":"Buenos Aires","Country":"Argentina"}, {"Name":"Centro comercial Moctezuma","City":"México D.F.","Country":"Mexico"}, {"Name":"Chop-suey Chinese","City":"Bern","Country":"Switzerland"}, {"Name":"Comércio Mineiro","City":"São Paulo","Country":"Brazil"} ] } 

这是我的文件php

Date          ClosingPrice
2017-3-16      10.00
2017-3-17      10.13
2017-3-20      10.19
    ...

提前感谢您的回复,

1 个答案:

答案 0 :(得分:0)

我认为你以你的方式尝试了这个简单的事情但由于输入错误而没有在控制器上获得任何数据:)

从HTML内部调用函数时,你犯了一个非常愚蠢的错误。您输入的内容为myFunction()根据您的控制器代码,调用名称应为myfunction()

所以,以下内容完美无缺!

<!DOCTYPE html>
<html>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.4.8/angular.min.js"></script>
<body>


<div ng-app="myApp" ng-controller="customersCtrl"> 

<table>
    <tr>
    <th>Nom</th>
    <th>Pays</th>
    <th>Ville</th>
    <th>button</th>
    </tr>
    <tr ng-repeat="x in myData">
    <td value="{{ x.Name }}">{{ x.Name | uppercase }}</td>
    <td>{{ x.Country }}</td>
    <td>{{ x.City }}</td>
    <td><button ng-click="myfunction(x.Name)">Click me!</button></td>
    </tr>
</table>

</div>

<script src="Ctrl.js"></script>

</body>
</html>

Ctrl.js如下......

var app = angular.module('myApp', []);
app.controller('customersCtrl', function($scope, $http) {
  $http.get("customers.php").then(function (response) {
      $scope.myData = response.data.records;
      console.log($scope.myData);
      $scope.myfunction = function(Name){
            console.log(Name);
      }
  });
});
  

Javascript区分大小写!