单击按钮应禁用angularjs中的div

时间:2016-01-04 09:08:50

标签: angularjs

我尝试在使用angularjs单击按钮时禁用div。单击主页按钮时,应通过保持剩余的div处于活动状态来禁用同一页面的一个div。

我怎样才能做到这一点?任何人都可以帮我解决这个问题...

我的HTML代码:

<button 
    style="margin-left: 22px; margin-top: 16px; background-color: #73AD21" 
    ng-click="getHome()">Home</button>

<div style="margin-top: 15px; display: inline-block" ng-repeat="imageSource in imageSources">
    <img width=40 height=50 style="margin-left: 12px;" ng-src="{{imageSource}}" /> <br> 
    <span style="margin-left: 12px;">{{getFilenameFromPath(imageSource)}}</span>
</div>

我的js代码:

$scope.imageSources = [];
$scope.imageSources.push('images/Open.png');
$scope.imageSources.push('images/New.jpg');
$scope.imageSources.push('images/Save.png');

$scope.getFilenameFromPath = function(filename) {
    return filename.split("/")[1].split(".")[0];
}
$scope.getHome = function() {

    window.location = "./Home.html";
}

2 个答案:

答案 0 :(得分:0)

根据你的命令,代码应该是;

<button 
    style="margin-left: 22px; margin-top: 16px; background-color: #73AD21" 
    ng-click="getHome()">Home</button>

<div ng-if="showDiv" style="margin-top: 15px; display: inline-block" ng-repeat="imageSource in imageSources">
    <img width=40 height=50 style="margin-left: 12px;" ng-src="{{imageSource}}" /> <br> 
    <span style="margin-left: 12px;">{{getFilenameFromPath(imageSource)}}</span>
</div>

你的js代码应该是;

$scope.showDiv = true;
$scope.imageSources = [];
$scope.imageSources.push('images/Open.png');
$scope.imageSources.push('images/New.jpg');
$scope.imageSources.push('images/Save.png');

$scope.getFilenameFromPath = function(filename) {
return filename.split("/")[1].split(".")[0];
}
$scope.getHome = function() {
    $scope.showDiv = false;
    window.location = "./Home.html";
}

答案 1 :(得分:0)

在div标签中写ng-disabled,在ng-click="noClickable()"写回if;

<div ng-disabled="disableDiv" ng-click = "noClickable()">....</div>

在控制器控制器

的功能中分配$ scope.disableDiv = true
 $scope.noClickable = function(){
         $scope.disableDiv = true;
         return false; 
 }

  $scope.getHome = function() {
        $scope.disableDiv = true;
        window.location = "./Home.html";
    }

请参阅此链接Click
我希望这会有用