Update parent scope variable in AngularJS

Malcr001

I have two controllers, one wrapped within another. Now I know the child scope inherits properties from the parent scope but is there a way to update the parent scope variable? So far I have not come across any obvious solutions.

In my situation I have a calendar controller within a form. I would like to update the start and end dates from the parent scope (which is the form) so that the form has the start and end dates when submitted.

Dan

You need to use an object (not a primitive) in the parent scope and then you will be able to update it directly from the child scope

Parent:

app.controller('ctrlParent',function($scope){
    $scope.parentprimitive = "someprimitive";
    $scope.parentobj = {};
    $scope.parentobj.parentproperty = "someproperty";
});

Child:

app.controller('ctrlChild',function($scope){
    $scope.parentprimitive = "this will NOT modify the parent"; //new child scope variable
    $scope.parentobj.parentproperty = "this WILL modify the parent";
});

Working demo: http://jsfiddle.net/sh0ber/xxNxj/

See What are the nuances of scope prototypal / prototypical inheritance in AngularJS?

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Update angularJs directive scope variable on change parent scope

From Dev

AngularJS : update parent scope from directive

From Dev

AngularJS : directive does not update scope after $http response in parent scope

From Dev

AngularJS binding variable outside of scope and notify update?

From Java

AngularJS directive does not update on scope variable changes

From Dev

AngularJS how to dynamically update part of scope with variable

From Dev

AngularJS binding variable outside of scope and notify update?

From Dev

Angularjs Update Scope variable within setTimeout not working

From Dev

How to update $scope variable value in AngularJs

From Dev

Parent id of scope Angularjs

From Dev

Parent id of scope Angularjs

From Dev

Angularjs update scope variable without re-repeating

From Dev

Angularjs update scope variable without re-repeating

From Dev

AngularJS directive scope variable doesnt update after reopening the window

From Dev

Variable scope or parent environment

From Dev

AngularJS/jQuery - Update Scope

From Dev

Scope variable not printing AngularJS

From Dev

Variable scope issue with AngularJS

From Dev

Scope variable not binding in AngularJS

From Dev

Dynamic scope variable in angularjs

From Dev

When is Isolated scope bound to parent scope in AngularJs?

From Dev

When is Isolated scope bound to parent scope in AngularJs?

From Dev

Access parent scope variable in ScalaTest

From Dev

Set variable in parent scope in Twig

From Dev

Angularjs directive two-way bound variable changes are not triggering $digest on the parent scope

From Dev

AngularJS animate div on scope update

From Dev

passing $scope variable to a function in AngularJS

From Dev

AngularJs: pass $scope variable with service

From Dev

Scope variable not accessible (undefined) - AngularJS

Related Related

  1. 1

    Update angularJs directive scope variable on change parent scope

  2. 2

    AngularJS : update parent scope from directive

  3. 3

    AngularJS : directive does not update scope after $http response in parent scope

  4. 4

    AngularJS binding variable outside of scope and notify update?

  5. 5

    AngularJS directive does not update on scope variable changes

  6. 6

    AngularJS how to dynamically update part of scope with variable

  7. 7

    AngularJS binding variable outside of scope and notify update?

  8. 8

    Angularjs Update Scope variable within setTimeout not working

  9. 9

    How to update $scope variable value in AngularJs

  10. 10

    Parent id of scope Angularjs

  11. 11

    Parent id of scope Angularjs

  12. 12

    Angularjs update scope variable without re-repeating

  13. 13

    Angularjs update scope variable without re-repeating

  14. 14

    AngularJS directive scope variable doesnt update after reopening the window

  15. 15

    Variable scope or parent environment

  16. 16

    AngularJS/jQuery - Update Scope

  17. 17

    Scope variable not printing AngularJS

  18. 18

    Variable scope issue with AngularJS

  19. 19

    Scope variable not binding in AngularJS

  20. 20

    Dynamic scope variable in angularjs

  21. 21

    When is Isolated scope bound to parent scope in AngularJs?

  22. 22

    When is Isolated scope bound to parent scope in AngularJs?

  23. 23

    Access parent scope variable in ScalaTest

  24. 24

    Set variable in parent scope in Twig

  25. 25

    Angularjs directive two-way bound variable changes are not triggering $digest on the parent scope

  26. 26

    AngularJS animate div on scope update

  27. 27

    passing $scope variable to a function in AngularJS

  28. 28

    AngularJs: pass $scope variable with service

  29. 29

    Scope variable not accessible (undefined) - AngularJS

HotTag

Archive