简体   繁体   English

如何将ngStyle传递给Angular中定义为元素的指令?

[英]How to pass ngStyle to directive defined as element in Angular?

I have custom directive (isolate scope) that uses some template. 我有使用某些模板的自定义指令(隔离范围)。 And I want to fire ng-style for internal template. 我想为内部模板ng-style

Here is a Demo that demonstrates the issue 这是演示该问题的演示


JS JS

app.controller('fessCntrl', function ($scope, Fct) {
   $scope.Fct = Fct;
});

app.$inject = ['$scope','Fct'];

app.factory('Fct', function() {
    return {
        theStyle: function(value) {
            return {'height': value*10 + 'px'}
        }
    };
});

app.directive('myElem',
   function () {
       return {
           restrict: 'E',
           replace:true,
           scope:{

           },
           template: '<div class="myclass"><input type="button" value=check></input>',
           link: function ($scope, $element, $attrs) {

               }
       }
   }) ;

I wrote HTML: 我写了HTML:

 <my-elem ng-style="Fct.theStyle(120)"> </my-elem>

but nothing happened. 但是什么也没发生。

How can I achieve to make external ng-style to work for directive template? 如何实现使外部 ng-style适用于指令模板?

The expected behavior should be similar like I'll write: 预期的行为应类似于我将要写的内容:

<div class="myclass" ng-style="theStyle(120)"><input type="button" value=check></input>

Thank you, 谢谢,

Your original fiddle is working with two minor change 您原来的小提琴正在进行两个小改动

  • change angular from 1.0.3 to v1.2 将角度从1.0.3更改为v1.2

  • your jsfiddle missed value parameter in theStyle function theStyle函数中的jsfiddle缺失value参数

jsfiddle: http://jsfiddle.net/vittore/9Ymvt/1591/ jsfiddle: http : //jsfiddle.net/vittore/9Ymvt/1591/

 <div ng-controller="fessCntrl"> 
   <my-elem ng-style="Fct.theStyle(12)"></my-elem>   
 </div>


 var app = angular.module('myModule', []);

app.controller('fessCntrl', function ($scope, Fct) {
   $scope.Fct = Fct;
});

app.$inject = ['$scope','Fct'];

app.factory('Fct', function() {
    return {
        theStyle: function(value) {
            return {'height': value*10 + 'px'}
        }
    };
});


app.directive('myElem',
   function () {
   return {
       restrict: 'E',
       replace:true,
       scope:{
       },
       template: '<div class="myclass">' +
                 '   <input type="button" value=check></input>' +
                 '</div>'

   }
}) ;

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

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