AngularJS按钮点击不起作用

时间:2015-10-29 17:40:21

标签: javascript html angularjs

的index.html

 <!DOCTYPE html>
<html ng-app="todoApp">
<head>
  <title>To Do List</title>
  <link href="skeleton.css" rel="stylesheet"/>
  <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.4.7/angular.min.js"></script>
  <script src="app.js"></script>
</head>
<body ng-controller="todoController">
  <div class="section hero">
    <div class="container">
      <div class="row">
        <div class="three columns">
            <li>
              <button class="button button-primary" ng-click="btnClick(first)">Data 1</button>
            </li>
        </div>
        <div class="nine columns">
          <h4>{{dataText}}</h4>
        </div>
      </div>
    </div>
  </div>
</body>
</html>

app.js

var app = angular.module('todoApp', []);
app.controller('todoController', function ($scope) {
      $scope.dataText = "";
      $scope.btnClick = function (num){
        dataText = "This is data from the " + num + " button."
      };
  });

当我点击按钮时,我希望ng-click启动并调用控制器中的功能,但当我点击没有任何反应时,屏幕上的文字不会发生变化或任何变化。我做错了什么?

1 个答案:

答案 0 :(得分:2)

你的html中没有名为first的变量,

 <li>
   <button class="button button-primary" ng-click="btnClick(first)">Data 1</button>
 </li>

而不是将其作为字符串传递

 <li>
   <button class="button button-primary" ng-click="btnClick('first')>Data 1</button>
 </li>

同样改变这样的行,

  $scope.btnClick = function (num){
    $scope.dataText = "This is data from the " + num + " button."
  };

这是工作Plunker