2015-11-04 33 views
1

我有一個選擇元素,我嘗試檢索使用ng模型選擇的值。它在我的應用程序頁面上正確顯示,但在應用程序控制器中它仍處於初始值。我一直在尋找答案,但我沒有找到任何令人滿意的東西。在選擇標記ng模型的問題

我的選擇:

<select ng-model= "choice.selected" ng-init = "choice.selected = historicOptions[0].id" ng-options = "historicOption.id as historicOption.value for historicOption in historicOptions">    
</select> 
<br> choice : {{choice.selected}} 

的選項和用於獲取所選值的變量(啓動爲缺省值):

$scope.historicOptions = [ 
    {id: 0, value: "month"}, 
    {id: 1, value: "week"}, 
    {id: 2, value: "day"} 
    ]; 
$scope.choice ={selected: 0}; 

編輯

這裏是我的角度應用程序(我在哪裏console.log())

var app = angular.module('AppEfergy', []); 

app.factory('historicFactory',[function(){ 
    var h = { 
    historic: [] 
    }; 

    h.createHistoric = function(choix){ 
    console.log("choix = ", choix); 
    if(choix == 0){ 
    return h.historic = [ 
    {date:"October" ,consumption:230}, 
    {date:"September" ,consumption:235}, 
    {date:"August" ,consumption:235}, 
    {date:"July"  ,consumption:240} 
    ]; 
    } 
    else if(choix == 1){ 
    return h.historic = [ 
    {date:"28/09/2015 - 04/10/2015" ,consumption:52}, 
    {date:"05/10/2015 - 11/10/2015" ,consumption:55}, 
    {date:"12/10/2015 - 18/10/2015" ,consumption:48}, 
    {date:"19/10/2015 - 25/10/2015" ,consumption:50}, 
    {date:"26/10/2015 - 01/11/2015" ,consumption:49} 
    ]; 
    } 
    else if(choix == 2){ 
    return h.historic = [ 
    {date:"01/11/2015" ,consumption:10}, 
    {date:"31/10/2015" ,consumption:11}, 
    {date:"30/10/2015" ,consumption:9 }, 
    {date:"29/10/2015" ,consumption:8 }, 
    {date:"28/10/2015" ,consumption:8 }, 
    {date:"27/10/2015" ,consumption:10}, 
    {date:"26/10/2015" ,consumption:9 } 

    ]; 
    } 

    }; 
    return h; 
}]); 


app.controller('MainCtrl', ['$scope','historicFactory', function($scope, historicFactory){ 
    $scope.choice ={selected: 0}; 
    $scope.choiceBis ={selected: 0}; 

    $scope.historicOptions = [ 
    {id: 0, value: "month"}, 
    {id: 1, value: "week"}, 
    {id: 2, value: "day"} 
    ]; 

    $scope.saveOptions = [ 
    {id: 0, value: -10}, 
    {id: 1, value: -5 }, 
    {id: 2, value: 5 }, 
    {id: 3, value: 10 } 
    ]; 

    $scope.currentValue = 237; 
    $scope.cost = 21; 
    $scope.saving = 3; 

    $scope.historic = historicFactory.createHistoric($scope.choice.selected); 
    console.log("choice : ", $scope.choice.selected); 


}]); 

和我的html頁面

<!DOCTYPE html> 
<html> 
    <head> 
    <title>Efergy App</title> 
    <meta charset="utf-8"/> 
    <link rel="stylesheet" href ="EfergyStyle.css"> 

    <!-- include Angular library--> 
    <script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.3.10/angular.min.js"></script> 

     <!-- include the AngularApp--> 
    <script src="AppEfergy.js"></script> 
    </head> 

    <body ng-app = "AppEfergy", ng-controller = "MainCtrl"> 
    <header> 
     Power Consumption 
    </header> 
    <div class = "currentConsumption"> 
     <br><h2>Current Consumption</h2> 
     <span class = "Value" ng-bind = "currentValue"></span> 
     <span class="unit">kW</span> 
    </div> 

    <br> 

    <div class = "historic"> 

     <h3>Historic</h3> 
     Display an historic by 
     <select ng-model= "choice.selected" ng-options = "historicOption.id as historicOption.value for historicOption in historicOptions"> 

     </select> 
     <br> choice : {{choice.selected}} 
    <br> 

     <table class = "historic"> 
     <tr> 
      <th>Date</th> 
      <th>Average consumption (kW)</th> 
     </tr> 
     <tr ng-repeat = "historics in historic"> 
      <td> 
      {{historics.date}} 
      </td> 
      <td> 
      {{historics.consumption}} 
      </td> 
     </tr> 
     </table> 

    </div> 
    </body> 
</html> 
+0

它是正確的嗎? http://jsfiddle.net/crndngdc/ – Errorpro

+0

它似乎工作代碼和正確的代碼 –

+0

它的工作原理,但是當我做consol.log(選擇)它總是打印0即使當我選擇另一個選項 – Manouk

回答

0

添加以下代碼控制器

$scope.getSelectedValue = function() { 
    console.log("choice : ", $scope.choice.selected); 
    $scope.historic = historicFactory.createHistoric($scope.choice.selected); 
}; 

對本次活動添加到您的選擇如下

ng-change="getSelectedValue()" 

在你的代碼沒調用您的選擇更改方法。這就是爲什麼你得到0作爲第一次控制器初始化控制檯品脫。

檢查plunker http://plnkr.co/edit/jPBnvn6FJVHXhdEQT3QO

+0

推遲的電話?我不明白。我的選擇是在一個HTML文件,我的歷史選項是在一個JS文件(我的角度應用),所以如果我是正確的應用程序做它的東西,當它被要求在HTML文件。不,如果我很清楚:/ – Manouk

+0

就是這樣!它的工作原理,多虧了你 – Manouk

0

您正在使用NG選項是當你想檢索所選型號的首選,但不是一個字符串值。你的例子顯示你只是期待所選項目的索引。

下面是代碼應該很好地工作:

<select ng-model= "selected" 
ng-options = "historicOption as historicOption.value for historicOption in historicOptions"></select> 
    <br> choice : {{selected.value}} 

這裏是控制器:

$scope.historicOptions = [ 
    {id: 0, value: "month"}, 
    {id: 1, value: "week"}, 
    {id: 2, value: "day"} 
]; 
$scope.selected =$scope.historicOptions[0]; 

這裏是plunker

更新: 現在,如果你只是想該id用作選項的值,值用作標籤,您只需使用即可與ng-repeat

<select ng-model="selected"> 
    <option ng-repeat="option in historicOptions" 
    value="{{option.id}}">{{option.value}}</option> 
</select> 
<br> choice : {{selected}} 

$scope.historicOptions = [ 
    {id: 0, value: "month"}, 
    {id: 1, value: "week"}, 
    {id: 2, value: "day"} 
    ]; 
$scope.selected =0; 
+0

就是這樣,我想要得到的id,因爲我想diplay表取決於這個id,這個值只是一個標籤,但所選的是很好地顯示在html頁面,但留在0角碼,所以它始終顯示相同的表 – Manouk

0

您不應該使用ng-init = "choice.selected = historicOptions[0].id",因爲這將始終顯示爲零索引值。所以刪除它並嘗試使用下面的代碼,如果你想下拉選擇的值,那麼你必須添加ng-change指令。

你的代碼看起來象

var app = angular.module('AppEfergy', []); 
 

 
app.factory('historicFactory', [ 
 
    function() { 
 
    var h = { 
 
     historic: [] 
 
    }; 
 
    h.createHistoric = function(choix) { 
 
     console.log("choix = ", choix); 
 
     if (choix == 0) { 
 
     return h.historic = [{ 
 
      date: "October", 
 
      consumption: 230 
 
     }, { 
 
      date: "September", 
 
      consumption: 235 
 
     }, { 
 
      date: "August", 
 
      consumption: 235 
 
     }, { 
 
      date: "July", 
 
      consumption: 240 
 
     }]; 
 
     } else if (choix == 1) { 
 
     return h.historic = [{ 
 
      date: "28/09/2015 - 04/10/2015", 
 
      consumption: 52 
 
     }, { 
 
      date: "05/10/2015 - 11/10/2015", 
 
      consumption: 55 
 
     }, { 
 
      date: "12/10/2015 - 18/10/2015", 
 
      consumption: 48 
 
     }, { 
 
      date: "19/10/2015 - 25/10/2015", 
 
      consumption: 50 
 
     }, { 
 
      date: "26/10/2015 - 01/11/2015", 
 
      consumption: 49 
 
     }]; 
 
     } else if (choix == 2) { 
 
     return h.historic = [{ 
 
      date: "01/11/2015", 
 
      consumption: 10 
 
     }, { 
 
      date: "31/10/2015", 
 
      consumption: 11 
 
     }, { 
 
      date: "30/10/2015", 
 
      consumption: 9 
 
     }, { 
 
      date: "29/10/2015", 
 
      consumption: 8 
 
     }, { 
 
      date: "28/10/2015", 
 
      consumption: 8 
 
     }, { 
 
      date: "27/10/2015", 
 
      consumption: 10 
 
     }, { 
 
      date: "26/10/2015", 
 
      consumption: 9 
 
     }]; 
 
     } 
 
    }; 
 
    return h; 
 
    } 
 
]); 
 

 

 
app.controller('MainCtrl', ['$scope', 'historicFactory', 
 
    function($scope, historicFactory) { 
 

 
    $scope.choice = { 
 
     selected: 0 
 
    }; 
 
    $scope.choiceBis = { 
 
     selected: 0 
 
    }; 
 

 
    $scope.historicOptions = [{ 
 
     id: 0, 
 
     value: "month" 
 
    }, { 
 
     id: 1, 
 
     value: "week" 
 
    }, { 
 
     id: 2, 
 
     value: "day" 
 
    }]; 
 

 
    $scope.saveOptions = [{ 
 
     id: 0, 
 
     value: -10 
 
    }, { 
 
     id: 1, 
 
     value: -5 
 
    }, { 
 
     id: 2, 
 
     value: 5 
 
    }, { 
 
     id: 3, 
 
     value: 10 
 
    }]; 
 

 
    $scope.getSelectedValue = function() { 
 
     console.log("choice : ", $scope.choice.selected); 
 
     $scope.historic = historicFactory.createHistoric($scope.choice.selected); 
 
    }; 
 

 
    $scope.currentValue = 237; 
 
    $scope.cost = 21; 
 
    $scope.saving = 3; 
 

 
    $scope.historic = historicFactory.createHistoric($scope.choice.selected); 
 
    console.log("choice : ", $scope.choice.selected); 
 
    } 
 
]);
<!DOCTYPE html> 
 
<html> 
 

 
<head> 
 
    <title>Efergy App</title> 
 
    <meta charset="utf-8" /> 
 
    <link rel="stylesheet" href="EfergyStyle.css"> 
 

 
    <!-- include Angular library--> 
 
    <script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.3.10/angular.min.js"></script> 
 

 
    <!-- include the AngularApp--> 
 
    <script src="AppEfergy.js"></script> 
 
</head> 
 

 
<body ng-app="AppEfergy" ng-controller="MainCtrl"> 
 
    <header> 
 
    Power Consumption 
 
    </header> 
 
    <div class="currentConsumption"> 
 
    <br> 
 
    <h2>Current Consumption</h2> 
 
    <span class="Value" ng-bind="currentValue"></span> 
 
    <span class="unit">kW</span> 
 
    </div> 
 
    <br> 
 

 
    <div class="historic"> 
 

 
    <h3>Historic</h3> 
 
    Display an historic by 
 
    <select data-ng-model="choice.selected" data-ng-change="getSelectedValue()" data-ng-options="historicOption.id as historicOption.value for historicOption in historicOptions"> 
 
    </select> 
 
    <br>choice : {{choice.selected}} 
 
    <br> 
 
    <table class="historic"> 
 
     <tr> 
 
     <th>Date</th> 
 
     <th>Average consumption (kW)</th> 
 
     </tr> 
 
     <tr ng-repeat="historics in historic"> 
 
     <td>{{historics.date}} 
 
     </td> 
 
     <td>{{historics.consumption}} 
 
     </td> 
 
     </tr> 
 
    </table> 
 

 
    </div> 
 
</body> 
 

 
</html>

+0

我試過你的解決方案,但它似乎並沒有解決我的問題,我做了一個console.log($ scope.choice.selected)在角碼中,它沒有得到我選擇的選項的價值 – Manouk

+0

好吧,讓我看看你的問題。我會盡快解決這個問題。 – Shohel

+0

你的申請是正確的。如果你的$ scope.choice = {selected:1},你只需要打印一次,所以控制檯的值是第一次。那麼控制檯值將是1.謝謝。 – Shohel

0

我想這是一個更大的解決方案的一部分(即它不只是一個選擇框和值)。您的模板可能會進行結構化,以便它啓動一個新的子範圍。

這意味着您的初始$scope.choice = { selected: 0 };不會執行任何操作來爲您的下拉菜單實際設置所選值(要確認這一點,請嘗試將所選值更改爲1)。取而代之的是,下拉值是通過0作爲第一項而設置的。

當您更改下拉值時,您只是在(原型繼承的)子範圍中創建屬性(並且不對父範圍中的now masked choice屬性執行任何操作)。

解決這個問題的最簡單方法是引用父範圍內的選擇屬性(使用$ parent.choice) - 這是否合適取決於其餘代碼。

下面是一個模板能說明問題

小提琴 - https://jsfiddle.net/0un8p2dh/