我想使用angularjs $ broadcast属性来隐藏我的表列。如果数据广播,则应显示表格列,否则应隐藏表格列。目前,如果我运行我的代码,一旦登录我广播我的登录用户名的功能,但仍然我的整个表不显示。我可以知道一种方法来解决它。
这是我的controller.js代码:
.controller('AllMovieController',
[
'$scope',
'dataService',
'$location',
'$rootScope',
function ($scope, dataService, $location, $rootScope){
$scope.noteEnabled = false;
$scope.movies = [ ];
$scope.movieCount = 0;
$scope.currentPage = 0; //current page
$scope.entryLimit = 20; //max no of items to display in a page
var getAllMovie = function() {
dataService.getAllMovie().then(
function (response) {
$scope.$on("passuser", function ($event, data){
if(data){
$scope.movies = response.data;
$scope.showSuccessMessage = true;
$scope.successMessage = "All movie Success";
$scope.noteEnabled = true;
}else{
$scope.movies = response.data;
$scope.noteEnabled = false;
}
});
},
function (err){
$scope.status = 'Unable to load data ' + err;
}
); // end of getStudents().then
};
$scope.numberOfPages = function(){
return Math.ceil($scope.movies.length/$scope.entryLimit);
};
getAllMovie();
}
]
)
这是我的部分的HTML代码:
<table class="table table-hover table-bordered">
<thead>
<tr>
<th>Title</th>
<th>Description</th>
<th ng-show="noteEnabled">Notes</th>
</tr>
</thead>
<tbody>
<tr ng-repeat="movie in movies | pagination: currentPage * entryLimit |
limitTo: entryLimit">
<td>
{{movie.title}}
</td>
<td>
{{movie.description}}
</td>
<td data-ng-click="selectFilmDetails($event,movie)" ng-show="noteEnabled" >
{{movie.comment}}
</td>
</tr>
</tbody>
</table>
这是广播控制器代码:
.controller('LoginController',
[
'$scope',
'dataService',
'$location',
'$window',
'$rootScope',
function ($scope, dataService, $location, $window, $rootScope){
$scope.check_login = function($event,userID,passwd){
dataService.login(userID,passwd).then(
function (response){
if(response.result.status=='ok'){
$scope.user = response.user;
$rootScope.$broadcast("passuser", $scope.user);
$location.path('#/home');
//$window.location.reload();
}else{
$scope.message = response.result.message;
}
},
function (err) {
$scope.status = 'unable to connect to data' + err;
}
);
}//end of function check_login
}
]
)
以前,我使用会话来检查用户是否登录,但现在我正在使用广播将用户名传递给控制台米勒。同样我试图将用户名传递给这个控制器它不工作。我真的需要帮助。提前致谢。
它仍然不适合我。我试图控制台登录数据,但我没有得到任何价值。它甚至没有进入scope.on函数 – anonymous5671
你可以更新你的问题,包括你调用'$ rootScope。$ broadcast()'的代码吗?你有没有调试过,以确保代码运行? – Lex
我已经添加了广播控制器请看看它 – anonymous5671