can't bind scope variables in angularjs

JoulinRouge

I have these files:

index.html:

<!DOCTYPE html>
<html>
<head>
    <title>Gestore Anagrafica</title>
</head>
<body>
    <div>
        <h3>Insert new person</h3>
        <div ng-app="Person" ng-controller="PersonController">
            First Name: <input type="text" ng-model="firstName"><br>
            Last Name: <input type="text" ng-model="lastName"><br>
            Age: <input type="number" ng-model="age"><br>
            <br>
            Full Name: {{fullName()}} <br>
            Is major: {{isMajor()}} <br>
            <button ng-click="add()">Add Person</button>
        </div>
    </div>
    <script type="text/javascript" src="js/angular.js"></script>
    <script type="text/javascript" src="js/RegistryService.js"></script>
    <script type="text/javascript" src="js/PersonController.js"></script>
</body>
</html>

js/RegistryService.js:

angular.module('RegistryService', []).

service('registry', function()
{
    this.people = [];

    this.add = function(person)
    {
        people.push(person);
    }
});

js/PersonController.js:

var app = angular.module('Person', ['RegistryService']);
app.controller('PersonController',['registry', function($scope, registry) {
    $scope.firstName = "testName";
    $scope.lastName = "";
    $scope.age = 20;

    $scope.isMajor = function()
    {
        return $scope.age > 18;
    };

    $scope.add = function()
    {
        registry.add({  'firstName': $scope.firstName,
                        'lastName': $scope.lastName,
                        'age': $scope.age});

    };

    $scope.fullName = function() 
    {
        return $scope.firstName + " " + $scope.lastName;
    };
}]);

The binding does not occur: when i change the name or the age nothing happens, moreover in the beginning the input are all blank but i expect to see 20 in age and testName in firstName. The browser's console shows no error. Am i missing something?

Constraints: the service 'RegistryService' MUST be in another file.

Question: with these two lines

var app = angular.module('Person', ['RegistryService']);
app.controller('PersonController',['registry', function($scope, registry) {

i am telling that the module named Person needs something called RegistryService to work, the controller PersonController will use the 'registry' thing located into the RegistryService (hence if i put here something that is not defined into RegistryService is an error). function($scope, registry) is the constructor of the controller that uses a global variable $scope and the variable registry taken from 'RegistryService'. Is my overall understanding of the dependency injection good?

dfsq

You need to describe $scope injection too, since you expect it to be available in your controller function as the first parameter:

var app = angular.module('Person', ['RegistryService']);
app.controller('PersonController', ['$scope', 'registry', function($scope, registry) { }]);

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Angularjs won't bind scope variable with function?

From Dev

Why doesn't AngularJS service bind to scope?

From Dev

Angularjs won't bind scope variable with function?

From Dev

Mobiscroll: can't bind the timepicker to the scope

From Dev

Can't bind $scope in controller when scope: {} is defined in the directive

From Dev

AngularJS doesn't bind ng-model to scope

From Dev

AngularJS doesn't bind ng-model to scope

From Dev

AngularJS ngMessages can't bind to $index expression

From Dev

Can't bind data to view angularjs

From Dev

AngularJs scope variables in console

From Dev

Testing AngularJS scope variables

From Dev

Can't bind to scope using vm. notation in controller/html

From Dev

Does .bind break $scope in angularjs?

From Dev

angularjs bind to controller with isolated scope

From Dev

AngularJS directive won't detect controller scope that binds variables using "this"

From Dev

AngularJS: accessing scope variables in $routeProvider

From Dev

Angularjs $scope variables not updating in view

From Dev

Angularjs $scope variables not updating in view

From Dev

AngularJS: accessing scope variables in $routeProvider

From Java

AngularJs can't access form object in controller ($scope)

From Dev

Firebase Database Function Can't Modify a $scope variable in angularjs

From Dev

AngularJS child scope bindings: behavior I can't explain

From Dev

angularjs: can't dynamically set filter method with scope data

From Dev

Bind AngularJS variables into CSS syntax

From Dev

Angular ng-bind not accessing scope variables

From Dev

Bind ngModel to AngularJS directive with isolated scope

From Dev

Bind element to existing AngularJS scope in a different frame

From Dev

AngularJS Bind service array variable to controller scope

From Dev

AngularJS : Bind to transclusive directive without isolating the scope

Related Related

  1. 1

    Angularjs won't bind scope variable with function?

  2. 2

    Why doesn't AngularJS service bind to scope?

  3. 3

    Angularjs won't bind scope variable with function?

  4. 4

    Mobiscroll: can't bind the timepicker to the scope

  5. 5

    Can't bind $scope in controller when scope: {} is defined in the directive

  6. 6

    AngularJS doesn't bind ng-model to scope

  7. 7

    AngularJS doesn't bind ng-model to scope

  8. 8

    AngularJS ngMessages can't bind to $index expression

  9. 9

    Can't bind data to view angularjs

  10. 10

    AngularJs scope variables in console

  11. 11

    Testing AngularJS scope variables

  12. 12

    Can't bind to scope using vm. notation in controller/html

  13. 13

    Does .bind break $scope in angularjs?

  14. 14

    angularjs bind to controller with isolated scope

  15. 15

    AngularJS directive won't detect controller scope that binds variables using "this"

  16. 16

    AngularJS: accessing scope variables in $routeProvider

  17. 17

    Angularjs $scope variables not updating in view

  18. 18

    Angularjs $scope variables not updating in view

  19. 19

    AngularJS: accessing scope variables in $routeProvider

  20. 20

    AngularJs can't access form object in controller ($scope)

  21. 21

    Firebase Database Function Can't Modify a $scope variable in angularjs

  22. 22

    AngularJS child scope bindings: behavior I can't explain

  23. 23

    angularjs: can't dynamically set filter method with scope data

  24. 24

    Bind AngularJS variables into CSS syntax

  25. 25

    Angular ng-bind not accessing scope variables

  26. 26

    Bind ngModel to AngularJS directive with isolated scope

  27. 27

    Bind element to existing AngularJS scope in a different frame

  28. 28

    AngularJS Bind service array variable to controller scope

  29. 29

    AngularJS : Bind to transclusive directive without isolating the scope

HotTag

Archive