1

im making a simple app, that can add a name and a description in a table. But im getting the error of - Cannot read property 'push' of undefined.

Could some one help me ?

this is my code.

HTML

<div id="main">
    <!-- angular templating -->
    <!-- this is where content will be injected -->
    <div ng-view>          
    </div>
</div>

JS

var scotchApp = angular.module('scotchApp', ['ngRoute','dx']);

scotchApp.config(function ($routeProvider, $locationProvider) {

$locationProvider.html5Mode({ enabled: true, requireBase: false });

$routeProvider

    // route for the home page
    .when('/', {
        templateUrl: '/pages/home.html',
        controller: 'mainController'
    })

    .when('/new', {
        templateUrl: '/pages/edit.html',
        controller: 'newController'
    });
})

scotchApp.controller('newController', function ($scope, $location) {

$scope.person = { name: "", description: "" };

$scope.save = function () {
    $scope.crew.push($scope.person);
    $location.path("/")
}

});

scotchApp.controller('mainController', function ($scope) {
$scope.crew = [
    { name: "Hugo", description: "Programador" },
    { name: "Vitor Lopes", description: "Técnico de Informática" },
    { name: "Pedro Sousa", description: "Webdesigner" },
]   
});

HTML - new.html ( page where i have my table)

 <table class="table table-striped" style="width: 350px;">
        <thead>
            <tr>
                <td><strong>Nome</strong></td>
                <td><strong>Descrição</strong></td>
                <td><a href="/new"><i class="glyphicon glyphicon-plus"></i></a></td>
            </tr>
        </thead>
        <tbody>
            <tr ng-repeat="person in crew">
                <td>{{person.name}}</td>
                <td>{{person.description}}</td>
                <td><i class="glyphicon glyphicon-edit"></i></td>
            </tr>
        </tbody>
    </table>

HTML - new.html ( page where i will add my new contact)

<form>
<input ng-model="person.name" placeholder="Enter Name"/><br />
<input ng-model="person.description" placeholder="Enter Description" /><br />
<button ng-click="save()" class="btn-primary">Save</button>

Thank You !!

5
  • you can't mix controllers like that? Commented Dec 28, 2015 at 16:02
  • i dont understand, sorry ! Commented Dec 28, 2015 at 16:03
  • The $scope variable in newController and mainController are ISOLATED scope variables. Commented Dec 28, 2015 at 16:06
  • Take a look at the many ways to share data across controllers. stackoverflow.com/questions/21919962/… Commented Dec 28, 2015 at 16:08
  • ok...and what do i do ? Commented Dec 28, 2015 at 16:09

1 Answer 1

2

define your array before push something like so :

$scope.crew = []
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks for the awnser, i guess my problem i because im pushing in an array that is in another controler

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.