Nonsono Statoio
Nonsono Statoio

Reputation: 111

Angular js SyntaxError: expected expression, got '.'

i'm trying to learn angular.js, specifically some try on the ng-if directives when i find this error in the console:

Angular js SyntaxError: expected expression, got '.' .controller('trovoilnome',function($scope){

i have assigned two controllers on the same module:

angular.module('direttive',[])
//per ng bind
.controller('addizione', ['$scope',function($scope){
    $scope.example = {
        numerouno: 12,
        numerodue: 7
    };
}]);

//per ng if
.controller('trovoilnome',function($scope){
    $scope.nome = {
        nome: 'Gigi',
        cognome: 'latrottola'
    };
}); 

somebody should tell me what i have done wrong?

Upvotes: 0

Views: 2533

Answers (2)

morels
morels

Reputation: 2105

1st solution:

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

//per ng bind
myapp.controller('addizione', ['$scope',function($scope){ 
    $scope.example = {
        numerouno: 12,
        numerodue: 7
    };
}]);

//per ng if
myapp.controller('trovoilnome',function($scope){
    $scope.nome = {
        nome: 'Gigi',
        cognome: 'latrottola'
    };
}); 

2nd solution:

angular.module('direttive',[])
       // per ng bind
       .controller('addizione', ['$scope',function($scope){ 
          $scope.example = {
            numerouno: 12,
            numerodue: 7
          };
       }])
       // per ng if
       .controller('trovoilnome',function($scope){
          $scope.nome = {
            nome: 'Gigi',
            cognome: 'latrottola'
          };
       }); 

The difference between the two solutions is that the second is a one-step declaration. The first one, instead, lets you split the definition of the two controllers in two different parts of the same file or also in two different files.

Ciao cumpa'

Upvotes: 1

tymeJV
tymeJV

Reputation: 104775

Remove the semi-colon after your first controller declaration:

}]); <--- REMOVE

//per ng if
.controller(

Upvotes: 5

Related Questions