0

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."
      };
  });

When I click the button, I would expect the ng-click to initiate and call the function in the controller, but when I click nothing happens, the text on the screen doesn't change or anything. What am I doing wrong?

4
  • 1
    What is 'first' referencing? ng-click="btnClick(first)" Commented Oct 29, 2015 at 17:43
  • Oh I meant to have it pass a string like "first" the string Commented Oct 29, 2015 at 17:45
  • 3
    In that case use ng-click="btnClick('first')" Commented Oct 29, 2015 at 17:46
  • I just changed it to that but for some reason it still doesn't work. Commented Oct 29, 2015 at 17:49

1 Answer 1

2

There is no variable named first in your html,

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

Instead of that, Pass it as a string

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

Also change the line like this,

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

Here is the working Plunker

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.