Change scope value on ng-click

2019-07-24 02:44发布

I would like to use tabs to hide and show items in an ng-repeat. Is it possible to change the value of a scope like so?

 <a ng-click="packageType = '1'">Package 1</a><a ng-click="packageType ='2'">Package 2</a><a ng-click="packageType = '3'">Package 3</a>
<div ng-repeat="item in packages" ng-show="packageType >=item.packageID">
{{item.name}}</div>

and the scope:

$scope.packages = [...{ "name": "some name",
                    "packageID": 1}...]

Where packageID can be 1, 2 or 3?

4条回答
【Aperson】
2楼-- · 2019-07-24 02:48

You code has a few problems:

You first ng-click is does not have a closing quote.

<a ng-click="packageType = '1'>Package 1</a>

You set packageType to a string when it is a number later:

packageType ='2'
$scope.packages = { "name": "some name",
                    "packageID": 1}

You don't need {{}} in the ng-show

ng-show="packageType >={{item.packageID}}"

I think you meant for packages to be an array instead of an object:

$scope.packages = { "name": "some name",
                    "packageID": 1}

Example of how to do it is below. I set it to show on the second and third links :

(function() {
  'use strict';

  angular
    .module('exampleApp', [])
    .controller('ExampleController', ExampleController);

  function ExampleController($scope) {
    var vm = this;
    $scope.packageType = 1;
    $scope.packages = [{
      "name": "some name1",
      "packageID": 2
    }, {
      "name": "some name2",
      "packageID": 2
    }, {
      "name": "some name3",
      "packageID": 2
    }, {
      "name": "some name4",
      "packageID": 3
    }, {
      "name": "some name5",
      "packageID": 3
    }, {
      "name": "some nam6",
      "packageID": 3
    }];
  }
  ExampleController.$inject = ['$scope'];
})();
<!DOCTYPE html>
<html ng-app='exampleApp'>

<head>
  <script src="https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.5.5/angular.min.js"></script>
</head>

<body ng-controller="ExampleController">
  <a ng-click="packageType = 1">Package 1</a>
  <a ng-click="packageType = 2">Package 2</a>
  <a ng-click="packageType = 3">Package 3</a>
  <p ng-repeat="item in packages" ng-show="packageType >= item.packageID">{{item.name}}
  </p>
</body>

</html>

查看更多
Evening l夕情丶
3楼-- · 2019-07-24 02:58

Here's the code that does exactly what your asking

Plunker

<!DOCTYPE html>
  <html ng-app="app">

  <head>
    <script src="//ajax.googleapis.com/ajax/libs/angularjs/1.0.7/angular.min.js"></script>
    <script src="http://angular-ui.github.com/bootstrap/ui-bootstrap-tpls-0.6.0.js"></script>
    <link href="//cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/2.3.2/css/bootstrap.css" rel="stylesheet">
  </head>

  <body ng-controller="MainCtrl">

    <div ng-controller="TabCtrl">
      <tabset>
        <tab ng-repeat="tab in tabs" heading="{{tab.name}}" active="tab.active" select="onTabSelected(tab.slug)">
          {{ tab.packageId }}
        </tab>
      </tabset>
    </div>
    <script type="text/javascript" charset="utf-8">
      angular.module('app', ['ui.bootstrap']).config(['$routeProvider', '$locationProvider',
      function($routeProvider, $locationProvider) {
          $routeProvider.when('/', {
              controller: 'MainCtrl'
          }).when('/room/:id', {
              controller: 'RoomCtrl',
          }).when('/dashboard', {
              controller: 'DashboardCtrl'            
          }).otherwise({
              redirectTo: '/'
          });
          $locationProvider.html5Mode(false);
      }]);    

      var TabCtrl = function($scope) {
        $scope.tabs = [{
          slug: 'dashboard',
          name: "Package 1",
          packageId: "some package #1"
        }, {
          slug: 'room-1',
          name: "Package 2",
          packageId: "some package #2"
        }, {
          slug: 'room-2',
          name: "Package 3",
          packageId: "some package #3"
        }];
      };

      RoomCtrl = function($scope, $location) {

      };

      DashboardCtrl = function($scope, $location) {

      };    

      MainCtrl = function($scope, $location) {

        $scope.onTabSelected = function(tab) {
          var route;
          if (typeof tab === 'string') {
            switch (tab) {
              case 'dashboard':
                route = tab;
                break;
              default:
                route = 'rooms/' + tab;
                break;
            }
          }
          $location.path('/' + route);
        };

      };
    </script>
  </body>

</html>
查看更多
Fickle 薄情
4楼-- · 2019-07-24 03:08

So I assume packages is an array and what you posted is not your complete code. You will want to change your ng-show to say

ng-show="packageType == item.packageID"

this will only show the div if the packageID is the same as the package type. Your ng-click should work fine. You will want to set a default value for packageType somewhere in your controller however so something shows initially

查看更多
对你真心纯属浪费
5楼-- · 2019-07-24 03:10

I would write a function that encapsulates what you want to happen on click. That would make it easier to understand.

查看更多
登录 后发表回答