简体   繁体   English

角向指令传递参数

[英]Angular passing parameter to directive

I m trying to pass some information through a view in a directive and it doesnt seem to work at all. 我试图通过指令的视图传递一些信息,它似乎根本不起作用。 In fact, even if I bind the scope, I have the string value writter : 实际上,即使我绑定了作用域,我也有字符串值writter:

Here's the directive 这是指令

angular.module('app')
    .directive('anomalieTableau', function () {

        var controller = ['$scope', '$attrs', 'srv_traitementsSite', function ($scope, $attrs, srv_traitementsSite) {
            $scope.anomalies = [];

            var idSite = $attrs.idsite;
            alert(idSite);
            var idAgence = $attrs.idagence;
            var dateDebut = $attrs.datedebut;
            var dateFin = $attrs.datefin;

            var promise = undefined;
            if (idAgence && idSite) {
                promise = srv_traitementsSite.TraitementSiteAgenceAnomalie(idSite, idAgence, dateDebut, dateFin);
            }

            promise.then(function (response) {
                $scope.anomalies = response.data;
            }, function (response) {

            });
        }];

        return {
            restrict: 'EAC',
            templateUrl: 'tpl/directive/AnomalieTableauDirective.html',
            controller: controller,
            scope: {
                idsite: '=',
                idagence: '=',
                datedebut: '=',
                datefin: '='
            }
        };
    });

Here's the HTML call : 这是HTML调用:

<div anomalie-tableau idsite="site._id" idagence="AgenceId" datedebut="dateDebutStats"
                     datefin="dateFinStats" class="col-md-12"></div>

And this is the alert result in the directive : 这是指令中的警报结果:

site._id site._id

Instead of : 代替 :

123456789 123456789

EDIT : Changing site._id by {{site._id}} in the attribute directive's call, it changes nothing and gives me this error : 编辑:在属性指令的调用中通过{{site._id}}来更改site._id,它什么也没有改变,并且给我这个错误:

Syntax Error: Token 'site._id' is unexpected, expecting [:] at column 3 of the expression [{{site._id}}] starting at [site._id}}]. 语法错误:令牌'site._id'是意外的,期望表达式[[{{site._id}}]的第3列中的[:]从[site._id}}开始。

What am I doing wrong ? 我究竟做错了什么 ?

Attributes are always strings. 属性始终是字符串。 So you will need to interpolate the value ( {{site._id}} ) and then possibly convert the string ( $attrs.idsite ) to your desired type. 因此,您将需要插值( {{site._id}} ),然后可能将字符串( $attrs.idsite )转换为所需的类型。

In respect to your scope-settings: You then have to use $scope instead of $attrs (and dont need the interpolation) since angular will copy those values to your scope. 关于您的范围设置:然后,您必须使用$scope而不是$attrs (并且不需要插值),因为angular会将这些值复制到您的范围中。 If you use = it will be a two-way-binding and you dont need to interpolate the values in your directive-attribute. 如果使用= ,它将是双向绑定,并且不需要在指令属性中插入值。

If you for some reason need to use $attrs you can do the interpolation yourself. 如果出于某种原因需要使用$attrs ,则可以自己进行插值。 Remove the scope-settings, use idsite="{{...}}" and inject the $interpolation Service into your directive. 删除范围设置,使用idsite="{{...}}"并将$ interpolation服务注入到您的指令中。

Then use $interpolate($attrs.idsite)($scope.$parent) to get the value (string). 然后使用$interpolate($attrs.idsite)($scope.$parent)获得值(字符串)。

http://plnkr.co/edit/zhBXyiz82EdmysLzeBNL?p=preview http://plnkr.co/edit/zhBXyiz82EdmysLzeBNL?p=preview

(note that in my plnkr I used @ in the scope-settings. You can remove that or leave it. With the @ angular will execute the interpolation for you and store the value in the $scope object of your directive.) (注意,在我的plnkr我用@在范围设置。您可以删除或离开它。随着@角将执行插值为您和存储在价值$scope的指令的对象。)

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

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