1

i am fiddling with angular and trying to understand when should i make a new controller or not. What i am trying to accomplish in angular is #1 randomizing a list when a user clicks the button. I can auto randomize, but not sure if on ng-click if that should require a new controller for the scope. and second thing i am trying to do is duplicate that list but when partner is clicked--it matches them with a random letter in the list. That involves appending another letter in the list to another. What I am wondering is, what if it is not even-what should happen to the extra letter? any suggestions. I think angular presents a cleaner way to solve problems then jquery.

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

.controller('myCtrl', ['$scope', function($scope) {
  $scope.list = ['a', 'b', 'c', 'd', 'e', 'f', 'g'];
  
  $scope.random = function() {
    return 0.5 - Math.random();
  };
   $scope.matchme = function() {
    alert("this");
  };


}]);
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>

<body ng-app="myApp">

<div ng-controller="myCtrl">
   <p ng-repeat="i in list">{{i}}</p>
  <p ng-repeat="x in list2">{{i}}</p>
  <button ng-click="random()">randomize</button>
<button ng-click="matchme()">partner</button>

</div>
Vzupo
  • 1,388
  • 1
  • 15
  • 34

1 Answers1

0

when should i make a new controller

Technically you can add any number of controllers you want. Best practice is 1 view = 1 controller for cleaner code.

randomizing a list when a user clicks the button

Your ng-click approach is correct. You just need to implement a randomizing algorythm on the array.

ng-click [...] require a new controller

No.

duplicate that list

In the matchme function use angular.copy on the array you want to duplicate.

when partner is clicked--it matches them with a random letter in the list [...]

From here on I don't understand your need.

Either way, for any new project / test, use the latest angular version. 1.2.23 is from 2014.

gyc
  • 4,300
  • 5
  • 32
  • 54