切换条件Angularjs与<a> tag

时间:2016-01-28 05:07:14

标签: html angularjs

I'm working on a code, where in i have icons under an < a > tag. Originally, I had a dropdown so showing a div on that was easy since a dropdown has a value but how do i code the same thing under an < a > tag?

2 个答案:

答案 0 :(得分:0)

如果您想在点击<a>时为变量指定值,请使用ng-click,而不是ng-model

<a href="#" ng-click="showX = !showX">X</a>
<div ng-if="showX">X</div>

答案 1 :(得分:0)

<强> HTML

<div ng-controller="appCtrl">
  <div>
        <a href="#" ng-click="assignVal('X')">X</a>
        <a href="#" ng-click="assignVal('Y')">Y</a>
        <a href="#" ng-click="assignVal('Z')">Z</a>         
</div>

        <div ng-if="showX">X --> div</div>
        <div ng-if="showY">Y --> div</div>
        <div ng-if="showZ">Z --> div</div>

  </div>   

<强>控制器:

 var appModule = angular.module('appModule', []);

    appModule.controller('appCtrl', function($scope) {

        $scope.assignVal = function(val) {          
            if (val === 'X') {
                $scope.showX = true;
                $scope.showY = false;
                $scope.showZ = false;
            } else if (val === 'Y') {
                $scope.showX = false;
                $scope.showY = true;
                $scope.showZ = false;
            } else if (val === 'Z'){
                $scope.showX = false;
                $scope.showY = false;
                $scope.showZ = true;
            }
        }
    });