AngularJS工厂里面的ng-class身体标签不起作用

时间:2015-05-27 05:37:32

标签: angularjs controller factory angular-services ng-class

被告知创建服务/工厂并在全局控制器上使用它。这是我第一次听到全球控制器。无论如何,我创建了一个名为Scroll的工厂并将其注入我们的main.controller.js。工厂中的函数是isScrollingEnabled,它返回true或false。另一个函数是setScrolling,它将变量设置为true或false。默认值设置为true。

在主控制器内部,我有这段代码

SELECT  [pid_code]
      ,[id_code]
      ,[description]
      ,[disp_order]
      ,[username]
      ,[user_key]
  FROM [webloan_helper].[dbo].[tbl_ledger_add]

该代码在控制台中吐出的是好的。

在我的模板上,我这样使用它。我有按钮将滚动设置为false。控制台中的值吐出错误,这很好。

$scope.scrollEnabled = Scroll.isScrollingEnabled();
console.log('value of $scope.scrollEnabled', $scope.scrollEnabled);

然而,它不起作用。如果我将其更改为下面编写的代码,则可以正常工作

<body ng-class="{ 'scrolling' : scrollEnabled }">

所以我猜,它不在范围内,尤其是ui-view在index.html中,main.controller.js和main.html将在ui-view中加载。 &lt;身体&gt;在此之前告诉我,main.controller.js中的任何范围都不会在ui-view之外工作。

那么解决方案是什么?

抱歉没有发布工厂。这是

<body ng-class="{ 'scrolling' : false }">

2 个答案:

答案 0 :(得分:3)

您要将值附加到的控制器的foreach(glob("http://www.externalsite.com/files/*xml") as $filename) { $xml_file = file_get_contents($filename, FILE_TEXT); echo $filename; } 不会扩展到$scope元素。相反,你可以将指令鞭在一起:

<body>

你将它贴在身上就像这样:

.directive('shouldScroll', function (Scroll) {
  return {
    restrict: 'A',
    link: function ($scope, elem) {
      $scope.$watch(Scroll.isScrollingEnabled, function (n) {
        if (n) {
          elem.addClass('scrolling');
        } else if (n === false) {
          elem.removeClass('scrolling');
        }
      });
    }
  };
});

答案 1 :(得分:0)

在某些情况下有效的另一种解决方案是仅使用class而不是ng-class:

<body class="{{ scrollEnabled ? 'scrolling' : '' }}">