2016-03-16 45 views
2

這是場景。我有兩個範圍數組,它們具有「排序」和「預先排序」的預定義值,並且它們不綁定在一起。我有按鈕,將值添加到「排序」數組和重置按鈕。單擊重置按鈕,以便「排序」數組的值將與「preSort」數組的值相同。然而,問題是,當我點擊重置。這兩個陣列現在被綁定。我想點擊復位按鈕在AngularJS上解除綁定數組

請參見下面的代碼後不綁定:

function isInArray(value, array) { \t 
 
     return array.indexOf(value) > -1; 
 
\t \t } 
 
\t \t function ascendingArrayList(value, array){ 
 
\t \t \t new_value = value.replace('-', '') 
 
\t \t \t index = array.indexOf(value); 
 
\t \t \t // alert(index); 
 
\t \t \t // Conditional statement is to make sure that it does not delete anything if field did not make any sorting yet 
 
\t \t \t // Comment the conditional to enable single sorting for each field 
 
\t \t \t if(index != -1){ 
 
\t \t \t \t array.splice(index, 1); 
 
\t \t \t } 
 
\t \t \t array.push(new_value); 
 
\t \t \t return array; 
 
\t \t } 
 
\t \t function descendingArrayList(value, array){ 
 
\t \t \t new_value = value.replace('-', '') 
 
\t \t \t index = array.indexOf(new_value); 
 
\t \t \t // Conditional statement is to make sure that it does not delete anything if field did not make any sorting yet 
 
\t \t \t // Comment the conditional to enable single sorting for each field 
 
\t \t \t if(index != -1){ 
 
\t \t \t \t array.splice(index, 1); 
 
\t \t \t } 
 
\t \t \t array.push(value); 
 
\t \t \t return array; 
 
\t \t } 
 

 

 
\t  var app = angular.module('myApp', []); 
 
\t \t app.config(function($interpolateProvider, $httpProvider) { 
 
\t \t \t // Change template tags 
 
\t \t  $interpolateProvider.startSymbol('[['); 
 
\t \t  $interpolateProvider.endSymbol(']]'); 
 
\t \t }); 
 

 
\t \t app.controller('myController', function($scope) { 
 
\t \t \t $scope.sorts = ["rank_order"]; 
 
\t \t \t $scope.preSort = ["rank_order"]; 
 
\t \t \t $scope.tableHeaders = [ 
 
\t \t \t \t {'name': 'No.', 'field':'-rank_order'}, 
 
\t \t \t \t {'name': 'Code'}, 
 
\t \t \t \t {'name': 'Description'}, 
 
\t \t \t \t {'name': 'Department', 'field':'-rank_department'}, 
 
\t \t \t \t {'name': 'Type', 'field':'-rank_type'}, 
 
\t \t \t \t {'name': 'Updated By', 'field':'-updated_by__bio__user_code'}, 
 
\t \t \t \t {'name': 'Date Updated', 'field':'-date_updated'}, 
 
\t \t \t ]; 
 
\t \t \t $scope.reset = function(){ 
 
\t \t \t \t 
 
\t \t \t \t $scope.sorts = $scope.preSort; 
 
\t \t \t } 
 

 
\t \t \t $scope.sort = function(field){ 
 

 
\t \t  \t if(isInArray(field, $scope.sorts)){ 
 
\t \t  \t \t $scope.sorts = ascendingArrayList(field, $scope.sorts) 
 
\t \t  \t }else{ 
 
\t \t  \t \t // $scope.sorts.push(field); 
 
\t \t  \t \t $scope.sorts = descendingArrayList(field, $scope.sorts) 
 
\t \t  \t } 
 
\t \t \t } 
 
\t \t });
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script> 
 
<div ng-app="myApp" ng-controller="myController"> 
 
\t <table border="1px"> 
 
\t <tr> 
 
\t \t <th ng-repeat="x in tableHeaders"> 
 
\t \t \t [[x.name]] <span ng-if="x.field" ng-click="sort(x.field, $event)"><u>Click to Sort</u></span> 
 
\t \t </th> 
 
\t \t </tr> 
 
    </table> 
 
\t preSort:[[ preSort ]] ------------------------------- sorts: [[ sorts ]] 
 
\t <br /> 
 
\t <button ng-click="reset()">Click to reset</button> \t 
 
</div>

+0

嘗試這個$ scope.reset =函數(){$ scope.sorts = [ 「rank_order」]; } –

+1

angular.copy竅門 –

回答

1

裏面$scope.reset()你不得不使用一些克隆陣列,而不是做一個分配範圍變量之間。

可以使用例如underscore的_.map功能

$scope.sorts = _.map($scope.preSorts, _.clone)