2015-10-20 69 views
1

我試圖來過濾angularjs搜索結果和我得到這個錯誤我的鉻控制檯上:錯誤:[NG:AREQ]AngularJS Filteriing搜索結果:錯誤:[NG:AREQ]

這是我的代碼片斷

<div id="notebooks" ng-controller="NotebookListCtrl"> 
    <input type="text" id="query" ng-model="query"/> 
    <select ng-model="orderList"> 
    <option value="name">By name</option> 
    <option value="-age">Newest</option> 
    <option value="age">Oldest</option> 
    </select> 
    <ul id="notebook_ul"> 
    <li ng-repeat="notebook in notebooks | filter:query | orderBy: orderList"> 
     name: {{notebook.name}}<br/> 
     procesor: {{notebook.procesor}}<br/> 
     <div class="right top">{{notebook.age}}</div> 
    </li> 
    </ul> 
    <span>Number of notebooks: {{notebooks.length}}</span> 
</div> 

這是我做了 http://plnkr.co/edit/P1DFr3fEuWiYGfC0k7bj?p=preview

一切似乎都不錯,但我不知道爲什麼我收到上述錯誤普拉克。

回答

0

定義控制器正確如下

更新:http://plnkr.co/edit/P1DFr3fEuWiYGfC0k7bj?p=preview

var app = angular.module("myApp",["ngSanitize"]); 

app.controller('NotebookListCtrl',['$scope',function($scope){ 
    $scope.notebooks = [ 
    {"name": "Lenovo", 
    "procesor": "Intel i5", 
    "age": 2011}, 
    {"name": "Toshiba", 
    "procesor": "Intel i7", 
    "age": 2010}, 
    {"name": "Toshiba", 
    "procesor": "Intel core 2 duo", 
    "age": 2008}, 
    {"name": "HP", 
    "procesor": "Intel core 2 duo", 
    "age": 2012}, 
    {"name": "Acer", 
    "procesor": "AMD", 
    "age": 2006}, 
    {"name": "Lenovo", 
    "procesor": "Intel i5", 
    "age": 2009}, 
    {"name": "Toshiba", 
    "procesor": "Intel i7", 
    "age": 2008}, 
    {"name": "Lenovo", 
    "procesor": "Intel i5", 
    "age": 2011}, 
    {"name": "Toshiba", 
    "procesor": "Intel i7", 
    "age": 2010}, 
    {"name": "Toshiba", 
    "procesor": "Intel core 2 duo", 
    "age": 2008}, 
    {"name": "HP", 
    "procesor": "Intel core 2 duo", 
    "age": 2012}, 
    {"name": "Acer", 
    "procesor": "AMD", 
    "age": 2006}, 
    {"name": "Lenovo", 
    "procesor": "Intel i5", 
    "age": 2009}, 
    {"name": "Toshiba", 
    "procesor": "Intel i7", 
    "age": 2008}, 
    {"name": "Lenovo", 
    "procesor": "Intel i5", 
    "age": 2011}, 
    {"name": "Toshiba", 
    "procesor": "Intel i7", 
    "age": 2010}, 
    {"name": "Toshiba", 
    "procesor": "Intel core 2 duo", 
    "age": 2008}, 
    {"name": "HP", 
    "procesor": "Intel core 2 duo", 
    "age": 2012}, 
    {"name": "Acer", 
    "procesor": "AMD", 
    "age": 2006}, 
    {"name": "Lenovo", 
    "procesor": "Intel i5", 
    "age": 2009}, 
    {"name": "Toshiba", 
    "procesor": "Intel i7", 
    "age": 2008} 
    ]; 

    $scope.orderList = "name"; 

}]); 
+0

@JnG,。是的,你可以,但你的重複。當然可以使用'ng-src'指令,它爲視圖中的img標籤提供圖像的路徑。 –

+0

請upvote我的問題。謝謝 – Blaze

0

的錯誤是

Argument 'NotebookListCtrl' is not a function, got undefined 

如果你點擊哪一個容易找到the url旁邊[NG:AREQ]在瀏覽器控制檯

你得到這個錯誤,因爲你沒有定義在控制器正確的方法。 1.2後AngularJS不允許「控制器爲全局函數」語法,而不是你需要把它添加到您的模塊,像這樣:

angular.module("myApp").controller('NotebookListCtrl',NotebookListCtrl); 

做微小,安全注射,你還應該做到以下幾點

NotebookListCtrl.$inject = ['$scope']; 
+0

看一看http://codepen.io/PageOnline/pen/nCfAj – Blaze

+0

該codepen沒有錯誤。 –

+0

我已經得到了解決方案,我的嘗試!謝謝 – Blaze

1
angular.module("myApp",["ngSanitize"]) 
.controller('NotebookListCtrl', ['$scope', 
    function ($scope) { 
    $scope.notebooks = [ 
     {"name": "Lenovo", 
     "procesor": "Intel i5", 
     "age": 2011}, 
     {"name": "Toshiba", 
     "procesor": "Intel i7", 
     "age": 2010}, 
     {"name": "Toshiba", 
     "procesor": "Intel core 2 duo", 
     "age": 2008}, 
     {"name": "HP", 
     "procesor": "Intel core 2 duo", 
     "age": 2012}, 
     {"name": "Acer", 
     "procesor": "AMD", 
     "age": 2006}, 
     {"name": "Lenovo", 
     "procesor": "Intel i5", 
     "age": 2009}, 
     {"name": "Toshiba", 
     "procesor": "Intel i7", 
     "age": 2008}, 
     {"name": "Lenovo", 
     "procesor": "Intel i5", 
     "age": 2011}, 
     {"name": "Toshiba", 
     "procesor": "Intel i7", 
     "age": 2010}, 
     {"name": "Toshiba", 
     "procesor": "Intel core 2 duo", 
     "age": 2008}, 
     {"name": "HP", 
     "procesor": "Intel core 2 duo", 
     "age": 2012}, 
     {"name": "Acer", 
     "procesor": "AMD", 
     "age": 2006}, 
     {"name": "Lenovo", 
     "procesor": "Intel i5", 
     "age": 2009}, 
     {"name": "Toshiba", 
     "procesor": "Intel i7", 
     "age": 2008}, 
     {"name": "Lenovo", 
     "procesor": "Intel i5", 
     "age": 2011}, 
     {"name": "Toshiba", 
     "procesor": "Intel i7", 
     "age": 2010}, 
     {"name": "Toshiba", 
     "procesor": "Intel core 2 duo", 
     "age": 2008}, 
     {"name": "HP", 
     "procesor": "Intel core 2 duo", 
     "age": 2012}, 
     {"name": "Acer", 
     "procesor": "AMD", 
     "age": 2006}, 
     {"name": "Lenovo", 
     "procesor": "Intel i5", 
     "age": 2009}, 
     {"name": "Toshiba", 
     "procesor": "Intel i7", 
     "age": 2008} 
    ]; 
    $scope.orderList = "name"; 
    } 
]); 
+0

你的答案是方法沒問題。 – Blaze