Changing $scope variable value in controller not changing value on view

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, ie "2222". I want to change it with "3333". I have tried with $scope.$apply() too.

Thanks.


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 div s 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.

链接地址: http://www.djcxy.com/p/77542.html

上一篇: 交叉控制器计算

下一篇: 在控制器中更改$ scope变量值不会更改视图上的值