简体   繁体   English

ngClass不会在父指令中应用更改

[英]ngClass doesn't apply changes in parent directive

i made two directives, one's exposing an API for another directive using controller. 我做了两个指令,一个是使用控制器为另一个指令公开一个API。

The child directive is a 'bodyElement' directive, and when clicked should update a class of the parent directive template. 子指令是一个“ bodyElement”指令,单击该指令应更新父指令模板的类。

While the modification of the parent $scope applies, the ngClass switch doesn't apply. 虽然对父$ scope的修改适用,但ngClass开关不适用。

Hope you can help: Directives: 希望能对您有所帮助:指令:

   .directive('humanBody', function () {

        return {
            transclude : true,
            scope: {},
            templateUrl: 'view1/template/human-body.tpl.html',
            controller: ['$scope', function ($scope) {
                $scope.form = {};

                $scope.body = {};
                $scope.body.selection = {};
                $scope.body.selection.head = true;
                $scope.body.selection.arm = false;
                $scope.body.selection.chest = false;
                $scope.body.selection.leg = false;


                $scope.isActive = function (type) {
                    return $scope.body.selection[type];
                };

                this.toggle = function (type) {
                    $scope.body.selection[type] = !$scope.body.selection[type];
                }


            }]
        }

    })


    .directive('bodyPart', function () {

        return {
            transclude : true,
            scope: {
                type: '@'
            },
            require: '^humanBody',
            link: function (scope, elem, attr, humanBody) {

                elem.on('click', function (event) {
                    console.info('toggle ' + scope.type);
                    humanBody.toggle(scope.type);
                });


            }

        }


    });

template of parent directive: 父指令模板:

i need that isActive(type) in ngClass switch between no-background <-> type-container when toggling (false/true). 我需要ngClass中的isActive(type)在切换(false / true)时在无背景<-> type-container之间切换。 It just work when rendering the page. 它仅在呈现页面时起作用。

<div class="container">
    <div class="row col-xs-12 body-part-container body-container">
        <div class="col-xs-12 "
             ng-class="{'no-background': !isActive('head'), 'head-container':isActive('head')}">
            <div class=" col-xs-12 arm-container"
                 ng-class="{'no-background': !isActive('arm'), 'arm-container':isActive('arm')}">
                <div class="col-xs-12  chest-container"
                     ng-class="{'no-background': !isActive('chest'), 'chest-container':isActive('chest')}">
                    <div class="col-xs-12 leg-container container"
                         ng-class="{'no-background': !isActive('leg'), 'leg-container':isActive('leg')}">
                        <body-part type="head"  class="head col-xs-12"></body-part>
                        <body-part type="arm"  class="arm col-xs-4"></body-part>
                        <body-part type="chest" class="chest col-xs-4"></body-part>
                        <body-part type="arm" class="arm col-xs-4"></body-part>
                        <body-part type="leg"   class="leg col-xs-12"></body-part>
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>

You need to kick off digest cycle in bodyPart directive, as you are updating scope variable from customEvent (updating angular context from outside world wouldn't intimate angular to run digest cycle to update view level bindings). 您需要在bodyPart指令中启动摘要循环,因为您要从customEvent更新范围变量(从外部世界更新角度上下文不会亲密角度来运行摘要循环来更新视图级别绑定)。

Code

elem.on('click', function (event) {
    console.info('toggle ' + scope.type);
    humanBody.toggle(scope.type);
    scope.$apply();
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM