javascript - How to use ng-class with a boolean from a service in angular.js ? - Stack Overflow

I want to set a class based on the boolean that I set in a service. This is a simplified version from m

I want to set a class based on the boolean that I set in a service. This is a simplified version from my code (for the sake of readability). The boolean is normally set by a lot of other functions in this service.

HTML :

<div ng-controller="MainController">
    <div ng-class="{ 'green' : MainController.CustomService.isGreen }">
    </div>
</div>

Service :

App.service("CustomService", function() {
    this.isGreen = true;
})

Controller :

App.controller('MainController', ['$scope', 'CustomService',  function($scope, CustomService) {

}]);

I want to set a class based on the boolean that I set in a service. This is a simplified version from my code (for the sake of readability). The boolean is normally set by a lot of other functions in this service.

HTML :

<div ng-controller="MainController">
    <div ng-class="{ 'green' : MainController.CustomService.isGreen }">
    </div>
</div>

Service :

App.service("CustomService", function() {
    this.isGreen = true;
})

Controller :

App.controller('MainController', ['$scope', 'CustomService',  function($scope, CustomService) {

}]);
Share Improve this question asked Apr 21, 2016 at 14:20 Jim PeetersJim Peeters 2,88310 gold badges35 silver badges55 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 4

Try this way:

App.controller('MainController', ['$scope', 'CustomService',  function($scope, CustomService) {
    $scope.isGreen = CustomService.isGreen;
}]);

HTML:

<div ng-class="{ 'green' : isGreen }">

View does not have direct access to service. View has access to $scope object, So if you need something in view, you shall write in $scope first.

If you want to track color:

App.controller('MainController', ['$scope', 'CustomService',  function($scope, CustomService) {
    $scope.isGreen = function () {
        return CustomService.isGreen;
    };
}]);

And:

<div ng-class="{ 'green' : isGreen() }">

Only properties of $scope are accessible to the view. So when you say MainController.CustomService.isGreen in the view, Angular tries to access $scope.MainController.CustomService.isGreen, which does not exist. You should publish the service to the scope in your controller.

App.controller('MainController', ['$scope', 'CustomService',  function($scope, CustomService) {
    $scope.CustomService = CustomService;
}]);

Then you can access your service from the view like this:

<div ng-class="{ 'green' : CustomService.isGreen }">
</div>

Another slightly different, more popular approach is to instruct the controller to publish itself in the scope. You do this by tweaking the ng-controller value to MainController as $ctrl (the name could be anything but Angular 1.5 standardized $ctrl). Then $ctrl bees available in your view:

<div ng-class="{ 'green' : $ctrl.CustomService.isGreen }">
</div>

In the controller function, $ctrl corresponds to this, so to publish the service, you would do:

App.controller('MainController', ['CustomService',  function(CustomService) {
        this.CustomService = CustomService;
}]);

Notice you don't need to inject $scope as a parameter now.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745442790a4627904.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信