2014-02-26 251 views
0

我試着去增加動態範圍$Angularjs動態添加控件

app.directive('dynamic', function ($compile) { 
return { 
    restrict: 'A', 
    replace: true, 
    link: function (scope, ele, attrs) { 
     scope.$watch(attrs.dynamic, function (html) { 
      ele.html(html); 
      $compile(ele.contents())(scope); 
     }); 
    } 
};}); 

和我的HTML;

<select dynamic="myOption_{{ item.RowId }}" id="select_{{ item.RowId }}" multiple class="chosen" data-ng-model="selectedOption" data-placeholder="Select" data-ng-change="ChangeTopic()" 
        chosen="directiveOptions" 
        ng-options="item.Id as item.tag for item in optionsFromQuery" 
        style="width:100%;"> 
      </select> 

angular;

var appsx = ""; 
      for (var i = 0; i < result.item.length; i++) { 
       var rowIds = parseInt(result.item[i].RowId); 
       appsx = '<option value="" selected>' + result.item[i].Tags + '</option>'; 
       var controlName = 'myOption_' + rowIds; 
       controlName = appsx; 
      } 

示例;如果我嘗試$ scope.myOption_100 = appsx;它的工作,但我不能動態範圍

$ scope.controlName ='myOption_'+ rowIds;像那樣。

回答

0

嘗試範圍。$觀察(attrs.dynamic .....

+0

我固定它現在 變種appsx = '<選項值= 「' + tagItem + '」 選擇= 「選擇」>' + tagItem +''; var rowIds = parseInt(result.item [i] .RowId); var controlName ='myOption_'+ rowIds; $ scope [controlName] = appsx; – user3207150