选择的值但它不会出现在输入中

时间:2015-07-09 08:24:49

标签: angularjs select coffeescript

我对select2有疑问。我选择了值,但在重新加载页面时它没有出现在输入中。我认为问题是范围的生命周期。

enter image description here

我的代码是下一个:

<select style="width:100%"
    class="configServices"
    ng-options="service.service_code as service.service_name group by service.parent for service in services"
    multiple="true"
    ng-model="widget.configuration.services">

我的cofeescript:

.directive 'selectServicesDirective', ['Services', '$translate', 'highchartsConfig',
  (Services) ->
restrict: 'ACE'
templateUrl: '/widgets/request-and-status/configuration/select-services.tpl.html'
link: (scope, element, attrs) ->
  scope.services = []
  scope.images = []
  scope.load = []
  scope.parent = []

  Services.status2 (services) ->
    services.data.forEach (service) ->
      if service.children
        service.children.forEach (children) ->
          scope.services.push {'service_name': children.service_name, 'service_code': children.service_code, 'parent': service.service_name}
          scope.images[children.service_name] = children.service_icon
      else
        scope.services.push {'service_name': service.service_name, 'service_code': service.service_code}
        scope.images[service.service_name] = service.service_icon

  scope.getParent = (serviceId) ->
    scope.parent.forEach (parent) ->
      if parent.id is serviceId
        console.log 'parent', parent.id
        return parent.id

  formatService = (service) ->
    return  $('<img style="height: 20px; width: 20px; border-radius:19px;" src="' +scope.images[service.text]+ '" /><b>'+' '+service.text+'</b>')

  $(".configServices").select2({
    maximumSelectionLength: 15,
    templateResult: formatService,
    templateSelection: formatService
  });

]

如果我把

 $(".configServices").select2({
    maximumSelectionLength: 15,
    templateResult: formatService,
    templateSelection: formatService
  }).val('3').change();

我可以看到输入中选择的值,但它返回错误:错误:[$ rootScope:inprog] $ digest已在进行中

1 个答案:

答案 0 :(得分:0)

  

我选择了值,但在重新加载页面时它没有出现在输入中。

重新加载页面时将重置页面状态。如果要在页面加载之间保留状态,则需要将该状态存储在缓存,本地存储或服务器端。