0

I wrote the following small angular code snippet:

html file:

<body ng-controller="myCtrl">
    <div class="container">
      ....
    </div>
</body>

javascript file:

app.controller('myCtrl', function($scope) {
  $scope.white_sld = get_init_white_sld();
  $scope.get_soldier_style = function is_soldier(loc) {

  var sld_color = get_soldier_color(loc);
  if (sld_color == "") {
      return ""; // no soldier in this square
  } else {
      return {
          'width': '80%', 'height': '80%', 'border-radius': '80%', 'background-color': sld_color,
          'margin': 'auto auto', 'vertical-align': 'middle'
      }
  }

  function get_soldier_color(loc) {
      for (var i = 0; i < white_sld.length; i++) {
          if ((loc[0] == white_sld[i][0]) && (loc[1] == white_sld[i][1])) {
              return "white"
          }
      }
      for (var i = 0; i < black_sld.length; i++) {
          if ((loc[0] == black_sld[i][0]) && (loc[1] == black_sld[i][1])) {
              return "black"
          }
      }
      return ""; // no soldier found
  }
 }
});

function get_init_sld_line(x_loc, y_loc, color) {
    var line_sld = [];
    for (var i = 0; i < 4; i++) {
        line_sld.push(new Soldier(x_loc + 2 * i, y_loc, color));
    }
    return line_sld;
}


function get_init_white_sld() {
    var init_white_sld = [];
    init_white_sld.push(get_init_sld_line(0, 0, "white"));
    init_white_sld.push(get_init_sld_line(1, 1, "white"));
    init_white_sld.push(get_init_sld_line(0, 2, "white"));
    return init_white_sld;
};

My problem with this code is that after the line "$scope.white_sld = get_init_white_sld();", $scope.white_sld gets the return value of the function get_init_white_sld(). However, when it gets inside the function 'is_soldier', then $scope.white_sld becomes undefined. My question is why and how can I resolve it?

9
  • 1
    What is get_init_white_sld? Commented Nov 11, 2016 at 19:23
  • I added it to the code snippet Commented Nov 11, 2016 at 19:25
  • 2
    get_init_sld_line does nothing. You declare an array, push to it, then do nothing with that array. You forgetting a return/ Commented Nov 11, 2016 at 19:26
  • And no, white-sld is not null: plnkr.co/edit/vW5c0e4ZDv9hsma4OIoZ Commented Nov 11, 2016 at 19:28
  • 1
    That's probably because you chose to name the first argument of is_soldier() $scope, thus hiding the actual controller $scope variable inside that function. Commented Nov 11, 2016 at 20:43

2 Answers 2

1

That's because you should use $scope.white_sld within that function, not just white_sld.

Sign up to request clarification or add additional context in comments.

Comments

0

I have updated the plunkr please check

Actually you forgot to add return statement in get_init_sld_line function.

https://plnkr.co/edit/kYebs1pFznaDpHlFDodK?p=preview

return line_sld;

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.