所以,这是我的HTML。
<input type="text" id="ghusername" ng-model="username" placeholder="Github username...">
<span id="ghsubmitbtn" ng-click="getUsers()">Pull User Data</span>
这是我的控制器A.
app.controller("homeController", ["$scope", "$http", function ($scope, $http) {
$scope.getUsers = function () {
$http.get("https://api.github.com/users/" + $scope.username)
.success(function (data) {
//some stuff
})
这是B(为了发布缘故)。如何在HTML ngModel上获取此用户名,以便我可以在另一个控制器中显示它?例如:
app.controller("reposController", ["$scope", "$http", function ($scope, $http) {
$scope.getRepos = function () {
$http.get("https://api.github.com/users/" + $scope.username + "/repos")
.success(function (data) {
// some stuff
})
};
我尝试过用户服务,工厂甚至是$ rootScopes,但它们似乎没有用,有什么帮助吗?顺便说一句,如果我不清楚告诉我,我会编辑帖子,谢谢。
编辑:我最终使用了$ rootScope,我知道这不是最好的主意,但这只是一个小问题。我会保留你的所有答案供你参考,因为我确信它们都能正常工作,但我实在是太笨了......谢谢。
答案 0 :(得分:0)
您必须将$ rootScope引用到您的控制器中:
app.controller("homeController", ["$scope", "$http","$rootScope", function ($scope, $http, $rootScope) ...
之后只需访问rootscope变量:
controller1:$rootScope.someValue = "some value";
Controller2:$scope.controllerScopeValue = $rootScope.someValue
;
答案 1 :(得分:0)
使用Label
service
然后将app.service('name', [function(){}])
添加到'name'
之类的
controllers
然后您可以像
一样访问它app.controller("reposController", ["$scope", "$http", 'name', function ($scope, $http, name) {
$scope.name = name;
和html
name.username