2016-07-21 8 views
0

属性値を取得し、私の場合、それは数字や配列かもしれないが、私のdirecttiveイムで文字列配列(例取得:「[1,2]」カスタムディレクティブ私のカスタムディレクティブイムで属性

方法私は、属性が文字列されていないで、私の配列を取得することができます

ビュー:?

<div my-directive to=[1,2] 

ディレクティブ:

angular.module('myApp') 
    .directive('myDirective', 
     [ 
      '$http', '$q','$uibModal', 
      dir 
     ]); 


function dir($http, $q, UserService, $uibModal) { 
    return { 
     restrict: 'A', 
     link: function($scope, element, attrs, controller) { 
      element.on('click', function(evt){ 


         console.log(attrs.to); 

      }); 
     } 
    }; 
} 

答えて

1

みてください、次のアプローチ:ディレクティブで

<div ng-app='demo'> 
    <demo-directive to-val='[1,2,3,4,5]'></demo-directive> 
</div> 

(ディレクティブ&は、ディレクティブのparamを設定INIT)

ビューで

var demo = angular.module('demo', []); 
demo.directive('demoDirective', function($parse) { 
    return { 
     restrict: 'E', 
     template: '<div ng-repeat="val in toVal">{{val}}</div>', 
     link: function (scope, element, attrs, controller) { 
      // parse the attribute array to a scope params 
      scope.toVal = JSON.parse(attrs.toVal); 
     } 
    } 
}); 
関連する問題