Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

TL;DR directive interpolates array as string..
http://plnkr.co/edit/ZwV8jmi0tUTgezWlEq3S?p=preview

Here is code:
index.html

<!DOCTYPE html>
<html data-ng-app="foo">

  <head>
    <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.16/angular.min.js"></script>
    <script src="app.js"></script>
  </head>

  <body data-ng-controller="FooCtrl">
    <ul data-foo="{{foo.bar}}">
      <li data-ng-repeat="bar in foo.bar">
        {{bar}}
      </li>
    </ul>
  </body>

</html>

app.js

!(function(ng) {
  'use strict';

  FooCtrl.$inject = ['$scope'];
  function FooCtrl($scope) {
    $scope.foo = {
      bar: [
        'foo',
        'bar'
      ]
    };
  }

  function FooDirective() {
    return {
      restrict: 'A',
      scope: {
        model: '@foo'
      },
      link: function($scope) {
        var watcher = function(model) {
          console.log(model, typeof model);
        };

        $scope.$watch('model', watcher, true);
      }
    };
  }

  ng.module('foo', [])
    .controller('FooCtrl', FooCtrl)
    .directive('foo', FooDirective);
})(window.angular);

Why do I have "typeof model" string instead of array?

share|improve this question

1 Answer 1

up vote 1 down vote accepted

If you want to bind the actual array of the parent scope use:

scope: {
  model: '=foo'
}

When using @foo you are just binding the result of the expression {{foo.bar}}. In your case the string interpretation of the array foo.

Addition

As an alternative you can call an expression on the parent scope from the isolated scope when you use the & symbol:

scope: {
  model: '&foo'
}

and evaluate it in your directive using:

model().bar
share|improve this answer
    
I don't wanted to use two-way binding because of additional watchers. So, looks like I have to use it. –  Miraage May 12 '14 at 7:44
    
@Miraage I added another option using & –  Sebastian May 12 '14 at 7:56

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.