想要获取标签,但视图中未显示任何内容

时间:2020-03-13 17:42:18

标签: javascript angularjs angularjs-select

函数被AngularJS中的表达式覆盖

我想将标签绑定到option标记中的selectedType值,但是视图中没有显示任何内容,并且在控制台中有很多调用

view.jsp

<select ng-if="RCDModel.id!=-1" class="form-control overloadDC"
        title="blablabla" disabled>
    <option value="{{RCDModel.selectedType}}"> 
      {{exprModelType(RCDModel.selectedType)}}
    </option>                                   
</select>

Controller.js

$scope.exprModelType = function(typeModel) {
    if (typeModel != undefined) {
        $scope.reunionType.forEach(function (type) {
            console.log("TYPE: "+type);
            if (type.id == typeModel) {
                console.log("TYPE ID: "+type.id);
                console.log("TYPE ID: "+typeModel);
                console.log("Libele: "+type.libelle);
                return type.libelle;
            }
        });
    }
    return "";
}

enter image description here

1 个答案:

答案 0 :(得分:1)

forEach块中的return语句不会将值返回给父函数。

代替使用array.find

$scope.exprModelType = function(typeModel) {
    if (typeModel != undefined) {
        var idMatch = $scope.reunionType.find( _ => _.id == typeModel.id );
        console.log("TYPE: "+idMatch); 
        console.log("TYPE ID: "+idMatch.id);
        console.log("TYPE ID: "+idModel);
        console.log("Libele: "+idMatch.libelle);
        return idMatch ? idMatch.libelle : "";
    }
    return "";
}

有关更多信息,请参见