0

I have shop attribute, which I want to treat differently, depending on what type passed into (it can be string or object). How do I properly bind this attribute, so that I could pass string and object, and then treat it differently, depending on the passed type?

This way object passing works perfectly fine, but passing a string gives this attribute a value '0':

angular.module('showcaseApp')
  .directive('card', function ($window, $state) {
    return {
      templateUrl: '/card.html',
      restrict: 'E',
      replace: true,
      scope: {
        shop: '='

      },
      link: function(scope, element, attrs) {
         attrs.$observe('shop', function (value) {
          if (value) {

          }
        });
      }
    };
  });

Binding with & allows me to pass a string (catch it with $observe), but an object can't be passed this way. And I want to be able to pass both. This can be solved only by creating a new attribute with different binding?

  • Possible duplicate of [What is the difference between & vs @ and = in angularJS](http://stackoverflow.com/questions/14908133/what-is-the-difference-between-vs-and-in-angularjs) – Sachila Ranawaka Mar 14 '17 at 06:23
  • could you not do a `typeof` check? and then do seomething with `string` vs `object`? – haxxxton Mar 14 '17 at 06:53
  • The problem is that I want to do it using one attribute, but I don't know how to pass value, object and srting seem to require different types of binding. After the value is passed, sure, I'll make a typeof check. – Ivan Vasiljev Mar 14 '17 at 07:09
  • If you do a `$scope.$watch("shop", ..` you should get the actual object/string that was passed – devqon Mar 14 '17 at 08:06

1 Answers1

0

Figured it out. I passed a string like this:

var id = scope.pageContent.content[i].id;

Instead I should pass it like this:

var id = "'" + scope.pageContent.content[i].id + "'";

And everything works ✨