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.

Jayram
  • 18,820
  • 6
  • 51
  • 68
Keval Patel
  • 925
  • 4
  • 24
  • 46

1 Answers1

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.

David Williams
  • 229
  • 2
  • 10
  • 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. – Keval Patel Feb 02 '15 at 09:48
  • You should do this, otherwise your tempdate value won't change. your href will always default to $scope.tempdate being initialized to 2222. – Harmony Proxy Mothibe Feb 02 '15 at 10:01
  • 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. – Keval Patel Feb 02 '15 at 17:15
  • 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: http://stackoverflow.com/a/11938785/4044584 – David Williams Feb 03 '15 at 00:56
  • Thank you, for now I have created different controller for different functionality. and it is working... thx – Keval Patel Feb 12 '15 at 12:19