3

I am making an angular controller and need to initialize an array with two empty objects so that two lines will appear on the screen and I need to initialize a new object in the array when a + button is clicked. I am unsure about how to go about doing this.

This is my best attempt:

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

myApp.controller('myController', ['$scope', '$q', function($scope, $q) {

    $scope.arr = [card('',''),card('','')];
    $scope.addLine = function(index){
        $scope.arr.push(card('',''));
    }
    $scope.removeLine = function(index){
        $scope.arr.splice(index, 1);
    }
}]);

function card(term, definition){
    this.term = term;
    this.definition = definition;
}
1
  • 2
    [new card('',''), new card('','')] Commented Nov 13, 2015 at 0:28

2 Answers 2

2

You need to use the keyword new to make an instance of card:

$scope.arr = [new card('',''), new card('','')];
//            ^                ^ See new keyword  
$scope.addLine = function(index){
    $scope.arr.push(new card('',''));
    //              ^ See new keyword
}

Also you should consider always Capitalize your constructors (This way you can indicate that its an constructor:

new Card('', '');
function Card(...) {...}

You can also boil the need of the new keyword away by checking with instanceof:

// No need for new anymore:
var card = Card(1, 2, 3); 
console.log(card instanceof Card); // true
function Card(a, b, c) {
  if (!(this instanceof Card)) return new Card(a, b, c);
  this.a = a;
  this.b = b;
  // ...
}
Sign up to request clarification or add additional context in comments.

2 Comments

Good to know. Thanks.
@Mardymar Glad I could help :-) Because javascript cannot give you compile errors it's always a good idea have coding conventions to minimize the amount of unexpected errors.
0
[new card('',''), new card('','')]

source

6 Comments

This does not provide an answer to the question. To critique or request clarification from an author, leave a comment below their post. - From Review
@L3viathan yes it does. I don't know what more you could possibly want.
Please consider editing your post to add more explanation about what your code does and why it will solve the problem. An answer that mostly just contains code (even if it's working) usually wont help the OP to understand their problem.
While this link may answer the question, it is better to include the essential parts of the answer here and provide the link for reference. Link-only answers can become invalid if the linked page changes. - From Review
@Drenmi if the OP is capable of reading the words then it will be clear how this source snippet answers the OP's problem. I'm sorry but I'm not writing a verbose answer just so it's easier to flip through posts in the "suspiciously short answer" queue and get your gold badge.
|

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.