显示和隐藏控制器不起作用,AngularJS

时间:2016-06-30 13:44:45

标签: angularjs controller ng-show angular-controller

我试图获取它以便#water div在点击水按钮时显示,而在点击能量按钮时则不显示。我正在尝试使用我制作的控制器,但到目前为止还没有用。

HTML

<head>


    <meta charset="utf-8">
    <title>test</title>
    <link rel="stylesheet" href="css/wisdom.css">
    <link rel="stylesheet" href="css/bootstrap.css">s
    <script src="http://code.jquery.com/jquery-1.12.2.min.js"></script>
    <script src="http://jquery-1.12.2.min.js"></script>
    <script src="js/bootstrap.min.js"></script>
    <script src = "http://ajax.googleapis.com/ajax/libs/angularjs/1.3.14/angular.min.js"></script> 
    <script src="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.1/js/bootstrap.min.js"></script> 


</head>

<body ng-app="WebApp">

    <div id="water" ng-include src="'menu.html'"></div>
    </br>
    </br>


    <section ng-controller="StateController">
    <div class="btn-group" role="group" ng-model="show" >
            <button type="button" class="btn btn-primary" ng-clicked="StateController.setState(1)">Water</button>
            <button type="button" class="btn btn-warning" ng-clicked="StateController.setState(2)">Energy</button>

        </br>
        </br> 
    <div ng-include src="'panels.html'" ng-show="StateController.isSet(1)"> </div>
    </div>
    </section>

    <script src="app.js"></script>
</body>

JS

var app = angular.module('WebApp', []);
app.controller('StateController', function(){
    this.state = 1;

    this.setState = function(selectedState){
        this.state = selectedState;
    };

    this.isSet = function(givenState){
        return this.state === givenState;
    };
});

1 个答案:

答案 0 :(得分:1)

进行以下更改

HTML

 <div id="water"  ng-show="something" ng-include src="'menu.html'"></div>
<button type="button" class="btn btn-primary" ng-click="setState(1)">Water</button>
            <button type="button" class="btn btn-warning" ng-click="setState(2)">Energy</button>

controller.js

var app = angular.module('WebApp', []);
app.controller('StateController', function($scope){
    $scope.setState=function(a){
          if(a==1)
            {
              $scope.something=true;
            }
          else{
                $scope.something=false;
               }
}
});

如果你想要显示水分区,那么在控制器中的setstate函数之前添加它

$scope.something=true;

如果你不想要那么把它变成假的