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('','')]CommentedNov 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; // ... } 
2
  • Good to know. Thanks.
    – Mardymar
    CommentedNov 13, 2015 at 0:52
  • @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.CommentedNov 13, 2015 at 0:54
0
[new card('',''), new card('','')] 

source

6
  • 1
    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
    CommentedNov 13, 2015 at 1:47
  • @L3viathan yes it does. I don't know what more you could possibly want.
    – djechlin
    CommentedNov 13, 2015 at 2:08
  • 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.
    – Drenmi
    CommentedNov 13, 2015 at 4:30
  • 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
    – ozil
    CommentedNov 13, 2015 at 7:21
  • @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.
    – djechlin
    CommentedJan 5, 2016 at 0:35

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.