Angular.js:从同一个ng-model中获取不同的{{display.value}}和$ scope.value形式

时间:2014-09-03 10:57:02

标签: javascript angularjs javascript-databinding

示例表格

<select ng-model="coffee.location">
    <option display-value='Some verbose message here' value="home">At home</option>
    <option display-value='Some verbose message here' value="work">At work</option>
    <option display-value='Some verbose message here' value="chain">Coffee Shop Chain (i.e. Starbucks &amp; Costa)</option>
    <option display-value='Some verbose message here' value="independent">Independent Coffee Shop</option>
</select>

显示元素示例:

<div>{{ coffee.location }}</div>

问题: 如何让我的Angular控制器接收option[value],同时{{coffee.location}}显示option[display-value](或者需要的属性)?

我希望我的问题很明确。提前干杯。

1 个答案:

答案 0 :(得分:0)

什么有效(由@dfsq提供)

相关的javascript:

app.controller('SuggestionController', ['$scope', function($scope) {
    $scope.stringMap = {
        home:   'cosy up at home',
        work:   'lounge at work',
        chain:  'patrol the highstreet'
    };
}]);

相关HTML:

<div id="some_parent" ng-app ng-controller="SuggestionController">
    ...
    <select ng-model="coffee.location">
        <option value="home">At home</option>
        <option value="work">At work</option>
        <option value="chain">Coffee Shop Chain</option>
        <option value="independent">Independent</option>
    </select>
    ...
</div>

{{stringMap[coffee.location]}}