在ng-click上更改范围值

时间:2016-07-15 00:53:56

标签: angularjs ng-repeat angular-ngmodel

我想使用标签来隐藏和显示ng-repeat中的项目。是否可以像这样更改范围的值?

 <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>

和范围:

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

哪里packageID可以是1,2或3?

4 个答案:

答案 0 :(得分:2)

这里的代码完全符合您的要求

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>

答案 1 :(得分:1)

我会编写一个函数来封装你想要点击的内容。这样可以更容易理解。

答案 2 :(得分:0)

所以我假设包是一个数组,你发布的不是你的完整代码。 您需要更改ng-show以说明

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

如果packageID与包类型相同,则只显示div。您的ng-click应该可以正常工作。您需要在控制器中的某个位置为packageType设置默认值,但最初会显示

答案 3 :(得分:0)

您的代码存在一些问题:

您首先点击是没有结束报价。

# Let's first fetch the last index of seq length
# last_index would have a scalar value
last_index = tf.shape(output)[1] 
# Then let's reshape the output to [sequence_length,batch_size,num_units]
# for convenience
output_rs = tf.transpose(output,[1,0,2])
# Last state of all batches
last_state = tf.nn.embedding_lookup(output_rs,last_index)

稍后将packageType设置为字符串:

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

您在ng-show

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

我认为你认为包是一个数组而不是一个对象:

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

如何操作的示例如下。我将其设置为显示在第二和第三个链接上:

&#13;
&#13;
$scope.packages = { "name": "some name",
                    "packageID": 1}
&#13;
(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'];
})();
&#13;
&#13;
&#13;