简体   繁体   English

为什么angularjs在select using指令中使用带有transclude和replace的空选项

[英]Why does angularjs include an empty option in select using directive with transclude and replace

I am creating a new directive for the select element. 我正在为select元素创建一个新指令。 As a best practice, I would like to receive some of the options from the server and one option I want to create in the client code eg "Search all cars". 作为最佳实践,我希望从服务器接收一些选项,并希望在客户端代码中创建一个选项,例如“搜索所有汽车”。

This is an example of how I want it to look: 这是我希望其外观的一个示例:

<select>
  <option value="">Search all cars</option>
  <option value="aud">Audi</option>
  <option value="bmw">BMW</option>
  <option value="maz">Mazda</option>
</select>

It is important that the value of "Search all cars" is empty. 重要的是“搜索所有汽车”的值为空。

But even though I have added an empty element in the select (ref other SO posts ) it still gives me an unwanted option: 但是,即使我在select中添加了一个空元素(请参阅其他SO posts ),它仍然给我一个不需要的选项:

<option value="?" selected="selected"></option>

Here is an plunker example of the issue/bug using an Angular directive with transclude and replace. 这是使用带有transclude和replace的Angular指令的问题/错误的一个小例子

Anyone got a suggestion of how to solve this? 任何人都提出了如何解决此问题的建议?

I have also added an issue to the angular team here . 我还在这里向角度小组添加了一个问题。

EDIT: 编辑:

The reason why I would like to have this as an directive is that I want to decide if I should have the "Search all cars" option or not depending on where it is implemented in my application. 我之所以要将此指令作为指令,是因为我想根据我的应用程序中的实现位置来决定是否应该使用“搜索所有汽车”选项。

EDIT2: Angular team confirms that this is a bug. EDIT2: Angular团队确认这是一个错误。 "It looks like the root reason is that if an unknown option is added after the initial setup, then it will not replace the generated unknown option" - lgalfaso . “看来,根本原因是,如果在初始设置后添加了未知选项,那么它将不会替换生成的未知选项” -lgalfaso

@Yoshi is right, it's to do with ngTransclude . @Yoshi是正确的,这与ngTransclude As soon as you move the default option into the directive itself, the problem goes away. 将默认选项移动到指令本身后,问题就消失了。 To work around this issue, since you don't care about the default value so much, you can modify the directive slightly and just import the text for the default option: 要变通解决此问题,由于您不太在乎默认值,因此可以略微修改指令,只需导入默认选项的文本即可:

app.directive('mySelect', [function () {
  return {
    restrict: 'E',
    replace: true,
    scope: {
      default: '@'
    },
    template: '<select ng-options="key as value for (key, value) in myMap"><option value="">{{ default }}</option></select>',
    link: function postLink(scope, element) {
      scope.myMap = {"bmw":"BMW","maz":"Mazda","aud":"Audi"}; // e.g. hashmap from server
    }
};

And then your HTML becomes: 然后,您的HTML变为:

<my-select ng-model="myModel" ng-change="doSomething(myModel)" default="Search all cars">
</my-select>

I just had the same issue as you and unfortunately @morloch's answer did not help me. 我只是遇到了与您相同的问题,很遗憾,@ morloch的回答没有帮助我。 In my case, the transclusion is dynamic. 就我而言,包含是动态的。

I started with your plunker and figured out that, if we use the transclusion during the prelink, it solves the issue. 我从您的unk客开始,发现,如果我们在预链接过程中使用包含功能,就可以解决问题。

   link: {
      pre:function preLink(scope, element, attrs, controllers, transcludeFn) {
        scope.myMap = {"bmw":"BMW","maz":"Mazda","aud":"Audi"}; // e.g. hashmap from server
        transcludeFn(scope, function(clone) {
          element.append(clone);
        });
      }
    } 

Here is my plunker : http://plnkr.co/edit/4V8r6iW2aKCLLbpIZc5e?p=preview 这是我的朋克: http ://plnkr.co/edit/4V8r6iW2aKCLLbpIZc5e?p=preview

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 为什么使用angularJs的select选项显示第一个空选项? - Why does select option using angularJs show first empty option? 为什么我的自定义AngularJS指令在select选项中不起作用? - Why does my custom AngularJS directive not work within a select option? AngularJS:使用transclude更改与指令关联的元素 - AngularJS : Change the element associated with directive using transclude 使用transclude AngularJS指令打破了$范围 - AngularJS directive using transclude breaks the $scope Angularjs:嵌入指令模板 - Angularjs: transclude directive template AngularJS:无法弄清楚为什么在使用transclude:&#39;element&#39;进行转换时嵌套的角度指令不被渲染 - AngularJS : Can't figure out why nested angular directive does not get rendered when transcluding with transclude:'element' 使用angularjs中的指令在下拉菜单中选择一个选项 - Select an option in dropdown using a directive in angularjs Angularjs:如何在使用ng-option时在select中包含空选项 - Angularjs: How to include empty option in select when using ng-option AngularJS在使用ng-repeat的html select中包含一个空选项-带有Angularjs 1.1.5的Umbraco后端 - Angularjs include an empty option in html select using ng-repeat - Umbraco back end with Angularjs 1.1.5 传递然后替换为angularjs - Transclude then replace with angularjs
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM