gpt4 book ai didi

javascript - Angularjs 仅在根状态下读取 $scope

转载 作者:行者123 更新时间:2023-11-28 18:26:43 25 4
gpt4 key购买 nike

我正在使用 ionicangularjs

我的应用程序。

.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中有这个( Root View ):

{{authHeader}}

我的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";
});

只有在根 Controller 中我才能获取$scope。我想添加另一个名为 login 的 Controller ,并将其更改为登录。有什么想法吗?

最佳答案

您可以使用 $rootScope 来实现此目的

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

在注册表中:

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

关于javascript - Angularjs 仅在根状态下读取 $scope,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38975016/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com