2015-01-16 60 views
0

當試圖做到這一點:angularjs類型錯誤:布爾不是一個函數NG單擊多個動作

<li class="answer"><a href="javascript:void(0)" ng-class="answer.show[$index] ? 'selected' : ''" ng-click="(answer.Questions.length > 0) ? (answer.show[$index]=!answer.show[$index]) (setAnswer(question, answer.ID)) : setAnswer(question, answer.ID)">{{answer.Description}}</a></li> 


TypeError: boolean is not a function 
    at $parseFunctionCall (http://localhost/assets/scripts/vendor/bower_components/angular/angular.js:12333:15) 
    at $parseTernary (http://localhost//assets/scripts/vendor/bower_components/angular/angular.js:12185:39) 
    at ngEventDirectives.(anonymous function).compile.element.on.callback (http://localhost/assets/scripts/vendor/bower_components/angular/angular.js:22949:17) 
    at Scope.$get.Scope.$eval (http://localhost/assets/scripts/vendor/bower_components/angular/angular.js:14383:28) 
    at Scope.$get.Scope.$apply (http://localhost/assets/scripts/vendor/bower_components/angular/angular.js:14482:23) 
    at HTMLAnchorElement.<anonymous> (http://localhost/assets/scripts/vendor/bower_components/angular/angular.js:22954:23) 
    at HTMLAnchorElement.eventHandler (http://localhost/assets/scripts/vendor/bower_components/angular/angular.js:3011:21)angular.js:11594 (anonymous function)angular.js:8544 $getangular.js:14484 $get.Scope.$applyangular.js:22954 (anonymous function)angular.js:3011 eventHandler 


The problem is on my ng-click: 

(answer.Questions.length > 0) ? (answer.show[$index]=!answer.show[$index]) (setAnswer(question, answer.ID)) : setAnswer(question, answer.ID) 

應該如何我寫這是否正確?

+2

不要把這麼多的邏輯視圖,更不用說表達。在控制器中創建一個範圍暴露的函數並調用它。 'NG點擊= 「doAllThisStuff($指數)」' –

回答

1

如果沒有必要,我不會在你的視圖中放太多東西。創建一個函數並傳遞你需要的東西並從那裏進行評估。

此外,

ng-click="(answer.Questions.length > 0) ? (answer.show[$index]=!answer.show[$index]) (setAnswer(question, answer.ID)) : setAnswer(question, answer.ID)" 

這看起來像一個錯誤的三元操作如果這是你想要做什麼?

如果正確地明白:

如果answer.Questions.length> 0,則(setAnswer(問題,answer.ID)),如果不是setAnswer(問題,answer.ID)。

這是什麼一部分

(answer.show[$index]=!answer.show[$index]) 

也許你想要的東西,像

((answer.Questions.length > 0) && (answer.show[$index]=!answer.show[$index])) ? (setAnswer(question, answer.ID)) : setAnswer(question, answer.ID) 
相關問題