Louis
Louis

Reputation: 2618

$scope.data undefined in controller

I can't use $scope.data in my controller, it's undefined, can you help please ?

Thanks

contact.html

<textarea cols="40" rows="6" name="message" id="message" class="panelInputs" placeholder="Message" ng-model="data.message" ng-change="displayscope()"></textarea>

controllers.js

angular.module('BoardLine.controllers', ['BoardLine.services'])
.controller('ContactCtrl', function($scope, $stateParams, sessionService, isPhoneGap, isIOS) {
  console.log($scope.data);
  $scope.displayscope = function() {
    console.log("displayscope : ");
    console.log($scope.data);
  }
})

app.js

angular.module('BoardLine', ['ionic', 'ngCookies', 'ui.unique', 'BoardLine.controllers', 'BoardLine.services', 'BoardLine.filters'])

Upvotes: 1

Views: 1621

Answers (2)

Daniel Kuta
Daniel Kuta

Reputation: 1634

In your controller you should declare $scope.date object

    angular.module('BoardLine.controllers', ['BoardLine.services'])
.controller('ContactCtrl', function($scope, $stateParams, sessionService, isPhoneGap, isIOS) {
//add this
$scope.data = {};

  console.log($scope.data);
  $scope.displayscope = function() {
    console.log("displayscope : ");
    console.log($scope.data);
  }
})

Upvotes: 2

Joy
Joy

Reputation: 9560

You need to define the property data like $scope.data = {} in your controller first. Because your ng-model binds to data.message, while data is undefined. So the message cannot be assigned.

Upvotes: 4

Related Questions