Angular中的动态css

Sim*_*cha 1 angularjs

嗨我现场有3种语言,其中一种有从右到左的字母,所以当我改变语言时,我需要动态更新css.不知道怎么做.问题是:我有ng-view,有语言,我更改语言,我有整个文档的HeadController,当我在ng-view中更改语言时,我的Head控制器看不到这种变化.在这里我的索引:

    <!doctype html>
<html lang="en" ng-app="ow" ng-controller="HeadCtrl">
    <head>
        <meta charset="utf-8">
        <meta name="viewport" content="width=100%, initial-scale=1, maximum-scale=1, minimum-scale=1, user-scalable=no"/>
        <title>OW</title>
        <link ng-repeat="stylesheet in stylesheets" ng-href="{{stylesheet.href}}" type="{{stylesheet.type}}" rel="stylesheet" />
        <script src="lib/angular/angular.min.js"></script>
        <script src="lib/jquery-1.9.1.min.js"></script>
        <script src="js/main.js"></script>
        <script src="js/app.js"></script>
        <script src="js/controllers.js"></script>
    </head>
    <body class="fontMain">
        <div ng-view></div>
    </body>
</html>
Run Code Online (Sandbox Code Playgroud)

和app.js.

angular.module('ow', []).
config(['$routeProvider', function($routeProvider) {
$routeProvider.
    when('/:placeId', {templateUrl: 'partials/menu.html',   controller: MenuCtrl}).
    when('/menu/:itemId', {templateUrl: 'partials/menu-details.html', controller: MenuItemCtrl}).
    when('/look/refill', {templateUrl: 'partials/refill.html', controller: RefillCtrl}).
    when('/look/orderCart', {templateUrl: 'partials/orderCart.html', controller: OrderCartCtrl}).
    when('/lang/:lang', {templateUrl: 'partials/menu.html', controller: LangCtrl}).
    otherwise({redirectTo: '/0'});
}]).controller("HeadCtrl", function($scope, sharedData) {
    if (sharedData.getLang() == "he") {
        $scope.stylesheets = [
            {href: "css/base.css", type: "text/css"},
            {href: "css/right.css", type: "text/css"}
        ];
    }
    else {
        $scope.stylesheets = [
            {href: "css/base.css", type: "text/css"},
            {href: "css/left.css", type: "text/css"}
        ];
    }
});
Run Code Online (Sandbox Code Playgroud)

sharedData - 包含共享值

这里语言控制器:

    // Controller for language changes
function LangCtrl($routeParams, $location, sharedData, $scope) {
    sharedData.setLang($routeParams.lang);
    $location.path("/");
}
Run Code Online (Sandbox Code Playgroud)

S M*_*han 6

你看过ng级吗?

http://docs.angularjs.org/api/ng.directive:ngClass

更详细一点:我认为你最好加载所有的css,然后根据你在运行时应用的动态选择.改变你加载的文件对我来说是错误的做法.

一个选项:使用ng-class动态更改BODY标记的类,并使规则与作为父级子级的元素匹配,并设置LEFT或RIGHT.