[英]Show/hide button using ng-show
I've got a problem related to the visibility of a button. 我有一个与按钮的可见性有关的问题。 I have to hide a button when there is no text in the form field, and show it back when it's filled.
当表单字段中没有文本时,我必须隐藏一个按钮,并在填充时将其显示回来。 I've got some code:
我有一些代码:
<div class="TextAreaCont">
<input ng-model="pageUrl" placeholder="Facebook Page URL" type="text">
</div>
<div class="ButtonCont" ng-show="ctrl.isButtonVisible()">
<button ng-click="ctrl.send()">Fetch data</button>
</div>
And I wrote it: 我写了:
Facebook.controller('PageCtrl', ['$scope', function($scope){
$scope.isButtonVisible = function(){
if($scope.pageUrl){return true}else{return false}
};
Where is the problem? 问题出在哪儿? To be honest, I've never leart JS and Angular.
老实说,我从未学习过JS和Angular。
try this. 尝试这个。 i put simple example.
我举一个简单的例子。
var app = angular.module('app', []); app.controller('TestController', function ($scope) { $scope.display = false; $scope.isButtonVisible = function(pageUrl){ if(pageUrl){ $scope.display = true; } else{$scope.display = false;} }; });
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script> <div ng-app="app"> <div ng-controller="TestController"> <input ng-model="pageUrl" placeholder="Facebook Page URL" type="text" ng-change="isButtonVisible(pageUrl)"> <div class="ButtonCont" ng-show="display"> <button ng-click="send()">Fetch data</button> </div> <input ng-model="pageUrl2" placeholder="Facebook Page URL" type="text" ng-change="isButtonVisible(pageUrl)"> <div class="ButtonCont" ng-show="pageUrl2"> <button ng-click="send()">Fetch data</button> </div> </div>
var Facebook = angular.module('Facebook', []);
Facebook.controller('PageCtrl', [function(){
var ctrl = this;
ctrl.isButtonVisible = function(){
if(ctrl.pageUrl){
return true;
}else{
return false;
}
};
ctrl.send = function(){
console.log('Page URL: ' + ctrl.pageUrl)
};
}]);
See Plunker for more details. 有关更多详细信息,请参见柱塞 。
Few problems that I can spot in your original code: 我可以在您的原始代码中发现的几个问题:
controller as
syntax, use 'this' keyword, not $scope
inside your controller controller as
语法时,请使用'this'关键字,而不是控制器内部的$scope
ng-model="pageUrl"
should be ng-model="ctrl.pageUrl"
ng-model="pageUrl"
应该是ng-model="ctrl.pageUrl"
Just FYI, isButtonVisible()
will be evaluated on every digest cycle, so there no need for watchers of ng-change
. 仅供参考,
isButtonVisible()
将在每个摘要周期进行评估,因此不需要ng-change
观察者。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.