2013-02-13 83 views
1

正如書名納克級的一個div類,我試圖設置在頁面加載(不點擊)具有角ng-class一個div一類,但我不知道如何或者如果可能的話。 div被封裝在一個模塊和控制器中,我之前試圖用$scope.frontpage設置該變量,但它不起作用。設置與頁面加載

<div id="main" role="main" class="clearfix" data-ng-module="ValidationBoxModule" ng-controller="ValidationBoxClassController"> 
    <div validation-id="dataValidationSummary" validation-summary ng-class="frontpage" ></div> 
    <asp:ContentPlaceHolder ID="MainContentPlaceHolder" runat="server"></asp:ContentPlaceHolder> 
</div> 

angular.module("ValidationBoxModule", []).controller("ValidationBoxClassController", ["$scope", function ($scope) { 

    $scope.frontpage = ""; 

    ($("#main").find(".sliderBox")) ? $scope.frontpage = "frontpage" : $scope.frontpage = ""; 
}]); 

那麼有沒有辦法做到這一點?

+0

你怎麼確定DIV? – 2013-02-13 16:49:57

+0

你使用jQuery嗎? – Jrod 2013-02-13 16:53:04

+0

jQuery也被加載,是 – Roland 2013-02-13 16:54:38

回答

3

你想要做的不是的角度方式。如:「不要在控制器中進行DOM操作」。而是使用一個指令,e.g:

function Ctrl($scope) { 
    $scope.frontpage = false; 
} 

angular.module('app', ['app.directives']); 

angular.module('app.directives', []).directive('isFrontpage', function() { 
    return { 
    restrict: 'A', 
    link: function (scope, element) { 
     scope.frontpage = angular.element(element).find('#main .sliderBox').length > 0; 
    } 
    }; 
}); 

有:

<body ng-controller="Ctrl" is-frontpage> 
    <div id="main"> 
    <div class="sliderBox"></div> 
    </div> 

    <div 
    validation-id="dataValidationSummary" 
    validation-summary 
    ng-class="{frontpage: frontpage}"> 
    </div> 
</body> 

演示http://jsbin.com/unihon/4/

+0

我仍然得到相同的行爲,什麼也沒有發生.... – Roland 2013-02-13 17:32:50

+0

它在演示中工作,也許你可以找出你的實際代碼的差異。 – Yoshi 2013-02-13 17:39:58

+0

我看到它,它必須是東西在我的代碼......這是一個大項目,這是不容易找到的東西... – Roland 2013-02-13 17:44:11