AngularJS/材质:为什么不对表达式求值? [英] AngularJS / Material: why the expression is not evaluated?

查看:75
本文介绍了AngularJS/材质:为什么不对表达式求值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不明白为什么在此代码中不对表达式"{{isMultiple}}"进行求值:

I do not understand why the expression "{{isMultiple}}" is not evaluated in this code:

HTML:

<md-input-container style="width: 30%;">
   <label>{{label}}</label>
   <md-select ng-model="choice" multiple="{{isMultiple}}">
     <md-option value="1">Option 1</md-option>
     <md-option value="2">Option 2</md-option>
     <md-option value="3">Option 3</md-option>
   </md-select>
 </md-input-container>

JS:

angular.module('app', ['ngMaterial'])
    .controller('AppCtrl', AppCtrl);

function AppCtrl($scope) {

    $scope.isMultiple = false;
    $scope.choice = "";
    $scope.label = "MyLabel";
}

Plunker上的完整代码: https://plnkr.co/edit/a5yCLW?p=preview

Full code on Plunker: https://plnkr.co/edit/a5yCLW?p=preview

在此示例中,下拉控件不应是多选的.

The Dropdown control should NOT be multi-selectable in this example.

推荐答案

不可能将表达式传递给属性multiple

It is not possible to pass an expression to the attribute multiple, according to the documentation:

但是,一种解决方法是在代码中创建md-select-

However, one way around this is to create the md-select in the code - CodePen

标记

<div ng-controller="AppCtrl" ng-cloak="" ng-app="MyApp">
<md-input-container id="myInputContainer" style="width: 30%;">
   <label>{{label}}</label>
 </md-input-container>
</div>

JS

angular.module('MyApp',['ngMaterial', 'ngMessages', 'material.svgAssetsCache', 'ngDialog'])

.controller('AppCtrl', function($scope, $element, $compile) {
  var myInputContainer = angular.element($element[0].querySelector('#myInputContainer')),
      mdSelect,
      mdSelectElement;

  $scope.options = [
    {value: 1, label: "Option 1"},
    {value: 2, label: "Option 2"},
    {value: 3, label: "Option 3"}
  ]
  $scope.isMultiple = false;
  $scope.choice = "";
  $scope.label = "MyLabel";

  mdSelect = "<md-select ng-model='choice' multiple='" + $scope.isMultiple + "'>" +
    "<md-option ng-repeat='option in options' value='{{option.value}}'>{{option.label}}</md-option>" +
    "</md-select>";
  mdSelectElement = angular.element(mdSelect);
  myInputContainer.append(mdSelectElement);
  $compile(myInputContainer)($scope);
});

这篇关于AngularJS/材质:为什么不对表达式求值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆