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
(根视图)中有这个:
Mycontroller.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}}
当控制器更改时,它将始终更新