首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >ui-select2 2内部指令绑定到外部作用域

ui-select2 2内部指令绑定到外部作用域
EN

Stack Overflow用户
提问于 2014-07-14 13:51:27
回答 1查看 392关注 0票数 0

我在指令中嵌套select2输入,并希望将选定的值绑定到外部范围。我怎么能这么做。柱塞实例

指令代码:

代码语言:javascript
复制
app.directive('optionChoices', function () {
  return {
    restrict: 'EA',
    scope: {
      type: '=',
      selections: '='
    },
    template: '<input ng-if="type === 1" ui-select2="textChoices" ' +
                'ng-model="selections" style="width:200px" />' +
                '<input ng-if="type === 2" ui-select2="colorChoices" ' +
                'ng-model="selections" style="width:200px" />' + 
                '{{\'inner:\' + selections}}',
    link: function (scope, element, attrs) {
      function Query(query) {
        var data={
            results:[]
        };
        if (query.term.length > 0) {
            data.results.push({id:query.term,text:query.term});
        }
        query.callback(data);
      }
      scope.textChoices = {
        query: Query,
        tags: [],
        initSelection: function () {}
      };
      scope.colorChoises = angular.extend({}, scope.textChoices, {
        formatSelection: function(state) {
            if (!state.id) return state.text;
            return "<div style='background-color:yellow;'>&nbsp;</div>" + state.text;
        }
      });
    }
  };
});
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2014-07-15 15:46:21

我发现了问题,也没那么难。在创建孤立作用域时,如果doens角将创建变量的单独实例,则不能仅绑定到父作用域。只需绑定到$parent,或绑定到和父对象:

代码语言:javascript
复制
scope: {
      option: '='
    }

在模板中:

代码语言:javascript
复制
template: '<input ng-if="option.type === 1" ui-select2="textChoices" ' +
                'ng-model="option.selections" style="width:200px" />' +
                '<input ng-if="option.type === 2" ui-select2="colorChoices" ' +
                'ng-model="option.selections" style="width:200px" />' + 
                '{{\'inner:\' + selections}}',

njoy!

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/24738093

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档