Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/405.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angularjs/21.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仅在根状态下读取$scope_Javascript_Angularjs_Ionic Framework_Angular Ui Router - Fatal编程技术网

Javascript Angularjs仅在根状态下读取$scope

Javascript Angularjs仅在根状态下读取$scope,javascript,angularjs,ionic-framework,angular-ui-router,Javascript,Angularjs,Ionic Framework,Angular Ui Router,我使用的是ionic和angularjs 我的应用程序 .state('auth', { url: '/auth', abstract:true, templateUrl: 'templates/auth.html', controller: 'authCtrl' }) .state('auth.reg', { url: '/reg', templateUrl: 'templates/register.html', controller: 'regCtrl' });

我使用的是
ionic
angularjs

我的应用程序

.state('auth', {
  url: '/auth',
  abstract:true,
  templateUrl: 'templates/auth.html',
  controller: 'authCtrl'
})
.state('auth.reg', {
  url: '/reg',
  templateUrl: 'templates/register.html',
  controller: 'regCtrl'
});
我在我的
auth.html
(根视图)中有这个:

My
controller.js

.controller('authCtrl', function($scope, $stateParams) {
  // here can get the scope
  // $scope.authHeader = "register";
})


.controller('regCtrl', function($scope, $stateParams) {
  // here CAN NOT get the scope
  $scope.authHeader = "register";
});
只有在根控制器中我才能获得
$scope
。我想添加另一个名为
login
的控制器,并将其更改为login。有什么想法吗?

试着介绍一个模型(参考对象)

这将由每个子状态继承,并且可以更改

用法将是

// {{authHeader}}
{{Model.authHeader}}
请参阅此处的更多详细信息:

或者

尝试引入模型(参考对象)

这将由每个子状态继承,并且可以更改

用法将是

// {{authHeader}}
{{Model.authHeader}}
请参阅此处的更多详细信息:


或者

您可以为此使用$rootScope

.controller('authCtrl', function($scope, $rootScope, $stateParams) {
  // here can get the scope
  // $rootScope.authHeader = "register";
})
.controller('authCtrl', function($scope, $rootScope, $stateParams) {
  // here can get the scope
  // $rootScope.headerData.authHeader = "register";
})
在reg中:

.controller('regCtrl', function($scope, $rootScope, $stateParams) {
  // here CAN NOT get the scope
  $rootScope.authHeader = "register";
});
.controller('regCtrl', function($scope, $rootScope, $stateParams) {
  // here CAN NOT get the scope
  $rootScope.headerData = {authHeader : "register"};
});

为此,可以使用$rootScope

.controller('authCtrl', function($scope, $rootScope, $stateParams) {
  // here can get the scope
  // $rootScope.authHeader = "register";
})
.controller('authCtrl', function($scope, $rootScope, $stateParams) {
  // here can get the scope
  // $rootScope.headerData.authHeader = "register";
})
在reg中:

.controller('regCtrl', function($scope, $rootScope, $stateParams) {
  // here CAN NOT get the scope
  $rootScope.authHeader = "register";
});
.controller('regCtrl', function($scope, $rootScope, $stateParams) {
  // here CAN NOT get the scope
  $rootScope.headerData = {authHeader : "register"};
});

为此,可以使用$rootScope

.controller('authCtrl', function($scope, $rootScope, $stateParams) {
  // here can get the scope
  // $rootScope.authHeader = "register";
})
.controller('authCtrl', function($scope, $rootScope, $stateParams) {
  // here can get the scope
  // $rootScope.headerData.authHeader = "register";
})
在reg中:

.controller('regCtrl', function($scope, $rootScope, $stateParams) {
  // here CAN NOT get the scope
  $rootScope.authHeader = "register";
});
.controller('regCtrl', function($scope, $rootScope, $stateParams) {
  // here CAN NOT get the scope
  $rootScope.headerData = {authHeader : "register"};
});
在您的模型中,请使用-

{{headerData.authHeader}}

当控制器更改时,它将始终更新。

为此,您可以使用$rootScope

.controller('authCtrl', function($scope, $rootScope, $stateParams) {
  // here can get the scope
  // $rootScope.authHeader = "register";
})
.controller('authCtrl', function($scope, $rootScope, $stateParams) {
  // here can get the scope
  // $rootScope.headerData.authHeader = "register";
})
在reg中:

.controller('regCtrl', function($scope, $rootScope, $stateParams) {
  // here CAN NOT get the scope
  $rootScope.authHeader = "register";
});
.controller('regCtrl', function($scope, $rootScope, $stateParams) {
  // here CAN NOT get the scope
  $rootScope.headerData = {authHeader : "register"};
});
在您的模型中,请使用-

{{headerData.authHeader}}
当控制器更改时,它将始终更新