gpt4 book ai didi

javascript - AngularJS 按钮 ng-click 不起作用

转载 作者:行者123 更新时间:2023-12-02 15:29:45 25 4
gpt4 key购买 nike

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 启动并调用 Controller 中的函数,但是当我单击时没有任何反应,屏幕上的文本不会更改或发生任何变化。我做错了什么?

最佳答案

您的 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

关于javascript - AngularJS 按钮 ng-click 不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33421104/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com