0

I'm using angular since 1 little week now and i have encounter a strange behavior (and i don't know why i didn't see that earlier because it's look like a pretty basic error).

I have a controller let's say HomeController which define two scope function : foo() and bar() and an object "var myVar = 5", I would like to change the value of myVar in foo() and write it's content in bar().

The problem is, it's look like that foo() does not modify myVar. I'm not still very aware about scopes in angular, and how they interact, but i think that my $scope in foo() is a different scope, a copy of the actual controller scope. so modifying it will not modify the controller one.

But how can i achieve this ?

Edit : Add all used code

Edit 2 : Add plunker : http://plnkr.co/edit/DGzw0UxjBeND3hQWK3o3

Here is my Index.html

<!-- index.html -->
<!DOCTYPE html>
<html>
<head>

    <!-- CSS -->
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/materialize/0.98.2/css/materialize.min.css">


    <!-- JS -->
    <script src="http://code.angularjs.org/1.2.13/angular.js"></script>
    <script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.2.13/angular-route.js"></script>


    <!-- Loading angular App -->
    <script src="app.js"></script>

    <!-- Loading angular controllers -->
    <script src="homeController.js"></script>

</head>


<body ng-app="TemplateApp">
    <div ng-view></div>
</body>
</html>

app.js :

var app = angular.module('TemplateApp', ['ngRoute']);

app.config(function($routeProvider) {

    $routeProvider
        .when('/', {
            templateUrl: 'home.html',
            controller: 'homeController'
        })

});

home.html :

<div class="row">
    <div class="col s6">
        <a class="btn" href="#" ng-click="foo()">call foo</a>
    </div>
    <div class="col s6">
        <a class="btn" href="#" ng-click="bar()">call bar</a>
    </div>
</div>

And finally homeController.js :

angular.module('TemplateApp').controller('homeController', [ '$scope', function($scope) {

    $scope.myVar = 5;

    $scope.foo = function() {
        $scope.myVar = 10;

        console.log($scope.myVar); // display 10
    };

    $scope.bar = function() {
        console.log($scope.myVar); // display 5 Should be 10
    };
}])

Thanks

3
  • as far as i can see - in current code, if you trigger "foo" before "bar" it should do exactly as you plan... i guess you problem is much more simpler than and have nothing to do with deep understanding of scopes Commented Jun 12, 2017 at 10:03
  • You need to share code of your view to understand what exactly you do. Commented Jun 12, 2017 at 10:29
  • I added my code and a plunker Commented Jun 12, 2017 at 12:41

4 Answers 4

1

From your plunker:

<div class="row">
    <div class="col s6">
        <a class="btn" href="#" ng-click="foo()">call foo</a>
    </div>
    <div class="col s6">
        <a class="btn" href="#" ng-click="bar()">call bar</a>
    </div>
</div>

Your clickable links contains href="#" that reloads page state and resets your scope variables, which is why bar() always sees value=5. Removing them should fix your problem.

Sign up to request clarification or add additional context in comments.

1 Comment

Ok, as i said, it was a stupid and pretty basic error :). It's work, thank you !
0

$scope.myVar will only be update when $scope.foo() method is called first.

Check below code, it should display 10:

angular.module('TemplateApp').controller('homeController', [ '$scope', function($scope) {

   $scope.myVar = 5;

    $scope.foo = function() {
        $scope.myVar = 10;

        console.log($scope.myVar); // display 10
    };

    $scope.bar = function() {
        console.log($scope.myVar); // display 5 , I would like 10 there
    };


    $scope.foo();
    $scope.bar();
}])

Comments

0

Tanks for these quick answer.

In fact i'm not calling these function from my controller.

I have a view file where i define two button the first

call foo call bar

Of course i click on the "call foo" btn first and then on the "call bar"

Comments

0

i placed two buttons which trigger each one its function:

<button ng-click="foo()">foo</button>
<button ng-click="bar()">bar</button>

look at this plunker - it shows 10 when "bar" clicked after "foo"

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.