Angularjs子节点无法更新父变量

时间:2016-02-05 16:54:50

标签: javascript angularjs

我有两个控制器:

app.controller('mainController',['$scope','$templateCache','$route','$location',function($scope, $templateCache,$route,$location) {

        $scope.clear = function(){
            //console.log("clicked");
            //console.log($route.current.originalPath);
            $( "#ActiveWarning" ).show();
            $templateCache.removeAll();
            $route.reload();
            $location.path( $route.current);
        }
}]);

并且

app.register.controller('settingsController', function ($scope) {
    $(document).ready(function(){
        $("#btnSubmitAccountSetting").click(function(e){
            e.preventDefault();
            swal({
                    title: 'Are you sure?',
                    text: 'Your account will be switched to unverified until you verify your account with updated documents.',
                    type: 'warning',
                    showCancelButton: true,
                    confirmButtonColor: '#3085d6',
                    cancelButtonColor: '#d33',
                    confirmButtonText: 'Yes, update it!',
                    closeOnConfirm: false
                },
                function() {
                    console.log("Clicked");
                    $scope.name = "toto";
                    $scope.$parent.name="toto";
                    console.log("all");
                    $scope.$digest();
                    $scope.$parent.name="toto";
                });
        });
    });
});

settingControllermainController的孩子。问题是我无法通过

更新我在mainController中设置的name变量 <data ng-controller="mainController" ng-init="name = '{{$BasicInfo["name"]}}'"/>

我尝试了$scope.$parent.name="toto";但没有帮助。我也厌倦了在mainController中添加setter但是也没有帮助。

只有子控制器中的变量正在更新,而不是父控制器。

如何从子控制器更新父控制器中的name变量。哪个在ng-view 指令

之内

html的结构

<!DOCTYPE html>
<html ng-app="app">
  <head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <title> Dashboard</title>
    <meta content="width=device-width, initial-scale=1, maximum-scale=1, user-scalable=no" name="viewport">
  </head>
  <body class="hold-transition skin-blue sidebar-mini" ng-controller="mainController">
    <data ng-controller="mainController" ng-init="name = 'Kheshav'"/>
    <dava ng-controller="mainController" ng-init="surname = 'Sewnundun'"/>   
<script src="http://maurijob.devlocal/libraries/JS/jquery-ui-1.11.1/jquery-ui.min.js"></script>
<script src="http://maurijob.devlocal/libraries/JS/angularjs/angular-1.4.8.min.js"></script>
<script src="http://maurijob.devlocal/libraries/JS/angularjs/angular-1.4.8-route.min.js"></script>
<script src="http://maurijob.devlocal/libraries/MyAngular/app.js"></script>
      <div class="content-wrapper"   ng-view>

    </div><!-- ./wrapper -->

</body>
</html>

更新

在控制器中删除了jquery:

app.register.controller('settingsController', function ($scope) {
    $scope.modal = function(){
            swal({
                    title: 'Are you sure?',
                    text: 'Your account will be switched to unverified until you verify your account with updated documents.',
                    type: 'warning',
                    showCancelButton: true,
                    confirmButtonColor: '#3085d6',
                    cancelButtonColor: '#d33',
                    confirmButtonText: 'Yes, update it!',
                    closeOnConfirm: false
                },
                function() {
                    console.log("Clicked");
                    $scope.name = "toto";
                    $scope.$parent.name="toto";
                    console.log("all");
                    $scope.$digest();
                    $scope.$parent.name="toto";
                });
    }

});

0 个答案:

没有答案
相关问题