0

I am working on Cordova tool and angularjs for my application.

cordovaApp.controller("VacationCtrl", function ($scope, $http, $location) {
        $scope.tempdate = "2222";
        $scope.ruleDetails = function () {
        $scope.tempdate = "3333";
    }
});

view 1

<div ng-controller="VacationCtrl">
    <a ng-repeat="data in rules" ng-click="ruleDetails()" class="summaryListBorder" href="#detailVacationRule">
    </a>
</div>

view 2

<div ng-controller="VacationCtrl">
    {{tempdate}}
</div>

In above given code, I sat value of $scope.tempdate to "2222". When I am click on link, it calls ruleDetails() and set $scope.tempdata = "3333". But when the new page is open with ng-view, it shows only old value, i.e. "2222". I want to change it with "3333". I have tried with $scope.$apply() too.

Thanks.

1
  • Why do you have two views with the same name "VacationCtrl"? Commented Feb 2, 2015 at 9:54

1 Answer 1

2

Every ng-controller attribute creates a new instance of the controller, which won't share the same scope as other instances. You want to wrap both divs in a single controller instance, like:

<div ng-controller="VacationCtrl">
    <div>
        <a ng-click="ruleDetails()" href="#detailVacationRule">
        </a>
    </div>

    <div>
        {{ tempdate }}
    </div>
</div>

If you need separate controllers, then you want to move common functions/fields into a service, which operates as a singleton so you can use it to share information between controllers. Or you could contain the separate controller instances in a parent controller, which will hold common fields and can be accessed through each controller's scope.

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

5 Comments

I can't do this. I have two different views for same controller. If I create {{template}} on the same view, then all the things will be shown up at a time.
You should do this, otherwise your tempdate value won't change. your href will always default to $scope.tempdate being initialized to 2222.
Thanks for replying, But still I am not clear with this. I want to open other view based on user clicks particular record. If you have any working example, then please suggest me.
With angular there are many ways to skin a cat, so it's hard to give you a solution without knowing your exact problem. Have a look at this other SO answer though and let me know if you're still unsure: stackoverflow.com/a/11938785/4044584
Thank you, for now I have created different controller for different functionality. and it is working... thx

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.