Javascript 在带有angularjs的页面上使用2个控制器
我试图为一个页面分配两个控制器,但我发现使用angular js很难完成 HTML 控制器Javascript 在带有angularjs的页面上使用2个控制器,javascript,html,angularjs,Javascript,Html,Angularjs,我试图为一个页面分配两个控制器,但我发现使用angular js很难完成 HTML 控制器 .controller('useraccount_ctrl',['$scope','$http',function($scope,$http){ $http.get('http://localhost/app/templates/user/user_account.php').success(function(data){ $scope.useraccount=(data) ;
.controller('useraccount_ctrl',['$scope','$http',function($scope,$http){
$http.get('http://localhost/app/templates/user/user_account.php').success(function(data){
$scope.useraccount=(data) ;
//console.log(JSON.stringify(data));
});
}])
.controller('userphotos_ctrl',['$scope','$http',function($scope,$http){
$http.get('http://localhost/app/templates/user/photos.php').success(function(data){
$scope.userphotos=(data) ;
console.log(JSON.stringify(data));
});
}])
我得到的结果只是控制器“useraccount\u ctrl”的结果有效,但userphotos\u ctrl控制器不使用允许您在状态定义中仅指定一个控制器,该控制器将动态链接到视图。有一些解决方案:
- 使用两个指令(请参阅)
- 使用静态导入手动导入控制器文件。您将能够从页面访问文件中的每个控制器:
问题是,使用UI路由器,您希望视图定义更加清晰,而不依赖静态导入(当您尝试构建好的应用程序时,静态导入会很混乱)。这就是为什么你应该选择第一个答案。这可能是一个复制/粘贴错误,但在模板中,控制器是“useraccount”,在JS中是“useraccount\u ctrl”。此外(也可以是一个错误),你在第一和第二部分都有
ng repeat=“item in userphotos”
,也许你需要ng repeat=“item in useraccount”
?刚刚纠正了打字错误。检查一次ng重复数据try$scope.userphotos=angular.fromJson(数据);请考虑在你投票时添加评论(我想知道为什么)。先谢谢你
.state('tabs.useraccount',{
url:'/useraccount',
views:{
'list-source':{
templateUrl: 'templates/user/user_account.html',
controller: 'useraccount_ctrl'
}
}
})
.controller('useraccount_ctrl',['$scope','$http',function($scope,$http){
$http.get('http://localhost/app/templates/user/user_account.php').success(function(data){
$scope.useraccount=(data) ;
//console.log(JSON.stringify(data));
});
}])
.controller('userphotos_ctrl',['$scope','$http',function($scope,$http){
$http.get('http://localhost/app/templates/user/photos.php').success(function(data){
$scope.userphotos=(data) ;
console.log(JSON.stringify(data));
});
}])