0

I have the following directive:

app.directive('skiTest', function($timeout,$compile) {
return {
  'replace': true,
  'restrict': 'E',
  'scope': {
    'data': '=',
    'selecto': '@',
      'ngModel': '='
  }, 
  link: function (scope, element, attrs) {
      attrs.$observe("selecto", function () {
          $timeout(function () {  // we need a timeout to compile after the dust has settled
              $compile(element.contents())(scope); //recompile the HTML, make the updated content work.
          },0);
      });
   },
    'template':'<div><select name="testSelect" ng-model="ngModel" ng-options="{{selecto}} in data"><option value="">Code</option></select></div>'
}

});

http://jsfiddle.net/L269zgbd/1/

If i'll try to select a country in the directive selection box, the ng-model object is being set to null.

Any idea why is that and how can i solve this problem?

Basically i want the same behavior on the directive selection as the one i get with the non directive selection.

Thanks!

2 Answers 2

1

If you upgrade the version of angular used in the fiddle the following works without using $compile or $timeout

app.directive('skiTest', function () {
    return {
        'replace': true,
            'restrict': 'E',
            'scope': {
            'data': '=',
                'selecto': '@',
                'ngModel': '='
        },
        'template': '<div><select name="testSelect" ng-model="ngModel" ng-options="{{selecto}} in data"><option value="">Code</option></select></div>'
    }
});

DEMO

Sign up to request clarification or add additional context in comments.

Comments

0

If you can't upgrade your version of angular like charlietfl suggests you can use the $compile function instead:

app.directive('skiTest', function($timeout,$compile) {
    return {
        'replace': true,
        'restrict': 'E',
        'scope': {
            'data': '=',
            'selecto': '@',
            'ngModel': '='
        }, 
        'template':'<div><select name="testSelect" ng-model="ngModel" ng-options="{{selecto}} in data" ng-change="changed()"><option value="">Code</option></select></div>',   
        compile: function(tElement, tAttributes){
            tElement[0].innerHTML = tElement[0].innerHTML.replace('{{selecto}}', tAttributes.selecto);
        }
    }});

jsfiddle: http://jsfiddle.net/r366r3b7/

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.