Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/asp.net-mvc/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript Angularjs子级无法更新父变量_Javascript_Angularjs - Fatal编程技术网

Javascript Angularjs子级无法更新父变量

Javascript Angularjs子级无法更新父变量,javascript,angularjs,Javascript,Angularjs,我有两个控制器: app.controller('mainController',['$scope','$templateCache','$route','$location',function($scope, $templateCache,$route,$location) { $scope.clear = function(){ //console.log("clicked"); //console.log($route.cu

我有两个控制器:

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);
        }
}]);

设置控制器
主控制器
的子控制器。问题是我无法更新通过

在主控制器中设置的
名称
变量

我尝试了
$scope.$parent.name=“toto”但没有帮助。我也很想在主控制器中添加二传手,但这也没用

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

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

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和angular。您应该在HTML本身上使用angular的
ng click
,而不是使用JQuery事件处理。或者一些指令。否则,angular的知识范围之外会发生一些事情,您必须开始处理
$digest
$apply
这些事情。我删除了jquery并使用了
ng click=
请检查更新。在父控制器上创建一个方法来更新值,然后从提供值的子控制器调用此方法。如创建
$scope.changename=function(newVal){$scope.name=newVal;}
然后从子控制器调用它,如
$scope.changename('value')@Neel我已经试过了,但不幸的是,它没有起作用。我对你的代码有强烈的怀疑。首先,你在controller中编写document.ready把jquery搞砸了。我还看到你在controller中分配了两次$scope.parent.name。你还说$scope.parent没有更新父作用域。
<!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>
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";
                });
    }

});