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.

I have the following directive

var notInModule = angular.module('ui.notIn', []);

notInModule.directive('notIn', function() { return { restrict: 'A', require: 'ngModel', link: function(scope, elm, attrs, ctrl) { var thisValue; var otherValues;

        attrs.$observe('notIn', function(values) {
            otherValues = values;
            validate();
        });

        ctrl.$parsers.unshift(function(viewValue) {
            thisValue = viewValue.toLowerCase();
            validate();
            return thisValue;
        });

        var validate = function() {
            ctrl.$setValidity('isWithin', otherValues.some(function(value) {
                return value.toLowerCase() === thisValue.toLowerCase();
            }));
        };
    },
};

});

and i am using it like this

<input ng-model="sample.name" not-in={{sample}} required/>

sample is an array on scope. However , in the directive 's controller , it comes in as a string.

for eg :if $scope.sample = ["a","b"] ,

In the directive , values is got as "["a","b"]"

share|improve this question
add comment

1 Answer

up vote 0 down vote accepted

Use angular.fromJson for conversion:

attrs.$observe('notIn', function(values) {
    otherValues = angular.fromJson(values);
    validate();
});
share|improve this answer
    
Works perfectly ! , but why is it getting converted into a string in the first place ? , can you explain me why that happens ? –  user1537766 Apr 30 at 8:06
    
Because all DOM attributes should have string as a value :). Think about this - Can you write an html markup, which attributes are of specific type? Actually no ! –  Engineer Apr 30 at 8:14
add comment

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.