我想写一个控制器来处理按钮点击“新建”按钮。目标是开始隐藏较低的代码块(开始<div ng-show="buttonClick">
)。当点击“新建”按钮时,我想让下面的代码块出现。AngularJS:按钮点击后显示html块
我遇到的问题是当我加载页面时,较低的代码块可见。它永远不会被隐藏。
{% extends "base.html" %}
{% block content %}
<script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.4.8/angular.min.js"></script>
<div class="container-fluid">
<div class="row">
<div class="col-sm-10 col-sm-offset-1">
<h2>Ratings</h2>
<table class="table table-bordered">
<thead>
<tr>
<th>Beer Name</th>
<th>Beer Rating</th>
<th>Notes</th>
<th>Brewer</th>
</tr>
</thead>
<tbody>
{% for rating in ratings %}
<tr>
<td>{{ rating.beer_name }}</td>
<td>{{ rating.score }}</td>
<td>{{ rating.notes }}</td>
<td>{{ rating.brewer }}</td>
<td><a href="{% url 'rating-edit' rating.id %}" class="btn btn-primary" value="{{ rating.id }}" name="edit">Edit</a></td>
<td><a class="btn btn-primary" href="{% url 'rating-delete' rating.id %}" value="{{ rating.id }}" name="delete" >Delete</a></td>
</tr>
{% endfor %}
</tbody>
</table>
<div ng-app="myApp" ng-controller="myCtrl">
<a ng-model="buttonClick" ng-click="is_clicked()" class="btn btn-primary">New</a>
</div>
</div>
</div>
<div ng-show="buttonClick" >
<div class="container-fluid">
<div class="row">
<div class="col-sm-10 col-sm-offset-1">
<h2>Enter a new rating</h2>
<form role="form" method="post">
{% csrf_token %}
<p>Beer Name: {{ form.beer_name }}</p>
<p>Score: {{ form.score }}</p>
<p>Notes: {{ form.notes }}</p>
<p>Brewer: {{ form.brewer }}</p>
<p><button type="submit" class="btn btn-primary">Submit</button></p>
</form>
</div>
</div>
</div>
</div>
</div>
<script>
var app = angular.module('myApp', []);
app.controller('myCtrl', function($scope){
$scope.buttonClick = false;
$scope.is_clicked = function() {
$scope.buttonClick=true;
console.log($scope.buttonClick)
}
})
</script>
{% endblock %}
什么不起作用? –
@GG。当我加载页面时,下面的块显示。它永远不会被隐藏。 – user3251349
浏览器控制台中是否有错误?如果你在你的控制器中添加一个'console.log($ scope.buttonClick)',记录什么? –