0

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?

0

2 Answers 2

5

Remove the semi-colon after your first controller declaration:

}]); <--- REMOVE

//per ng if
.controller(
Sign up to request clarification or add additional context in comments.

3 Comments

This is the solution, I'd also like to point out to OP the alternative of assigning the result of angular.module(. . . to a variable, and then calling myApp.controller(. . . which some people find cleaner.
just tried, but i have the same error in the console :-(
@NonsonoStatoio -- Update your question with the latest code. Did you clear cache and what not?
1

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'

Comments

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.