优化开关盒角度

时间:2016-07-16 14:24:50

标签: javascript angularjs

我的anguar控制器中有以下功能,在开关盒中有很多重复,我不知道如何优化它?

root@******:/var/projects# g++-5 -std=c++1y test.cpp
test.cpp: In function ‘int main(int, char**)’:
test.cpp:12:22: error: no matching function for call to ‘test1(std::nullptr_t)’
  test1<float>(nullptr);
                      ^
test.cpp:5:7: note: candidate: template<class ... Args> void test1(const std::function<void(Args ...)>&)
  void test1(const std::function<void(Args...)> &function)
       ^
test.cpp:5:7: note:   template argument deduction/substitution failed:
test.cpp:12:22: note:   mismatched types ‘const std::function<void(Args ...)>’ and ‘std::nullptr_t’
  test1<float>(nullptr);
                      ^
test.cpp:13:17: error: no matching function for call to ‘test1(void (*)(float))’
  test1<float>(&t);
                 ^
test.cpp:5:7: note: candidate: template<class ... Args> void test1(const std::function<void(Args ...)>&)
  void test1(const std::function<void(Args...)> &function)
       ^
test.cpp:5:7: note:   template argument deduction/substitution failed:
test.cpp:13:17: note:   mismatched types ‘const std::function<void(Args ...)>’ and ‘void (*)(float)’
  test1<float>(&t);
                 ^
test.cpp:16:3: error: no matching function for call to ‘test1(main(int, char**)::<lambda(float)>)’
  });
   ^
test.cpp:5:7: note: candidate: template<class ... Args> void test1(const std::function<void(Args ...)>&)
  void test1(const std::function<void(Args...)> &function)
       ^
test.cpp:5:7: note:   template argument deduction/substitution failed:
test.cpp:16:3: note:   ‘main(int, char**)::<lambda(float)>’ is not derived from ‘const std::function<void(Args ...)>’
  });
   ^

案例一直持续到 $scope.start = function() { var when; console.log("Start: " + $scope.level + " " + $scope.round) switch($scope.level + "|"+ $scope.round){ case "1|1": $scope.promptRound='Level 1 Round 1'; when=3000 break; case "1|2": $scope.promptRound='Level 1 Round 2'; when=3000 break; ... case "2|1": $scope.promptRound='Level 2 Round 1'; when=3000 break; case "2|2": $scope.promptRound='Level 2 Round 2'; when=3000 break; ... default: $scope.promptRound='Default'; } 基本上有几个级别由$ scope.level标识,每个级别有几个轮次(每个级别有不同的轮数)由$ scope.round标识。

1 个答案:

答案 0 :(得分:1)

这怎么转换?

$scope.promptRound = $scope.level && $scope.round 
  ? ['Level', $scope.level, 'Round', $scope.round].join(' ')
  : 'Default';

var when = $scope.level && $scope.round ? 3000 : null;

if($scope.level && $scope.round) {
  var when = 3000;
  $scope.promptRound = ['Level', $scope.level, 'Round', $scope.round].join(' ');
} else {
  $scope.promptRound = 'Default'; 
}