2016-04-20 109 views
1

如何在角JS中定義一個函數我正在嘗試一個函數,但是當我加載我的頁面時,它在控制檯SimpleController()中說不是函數。可B I正在做一些語法錯誤我們如何使用Angular JS在控制器中定義一個函數

HTML

<div ng-app> 

name: <input type="text" ng-model="name"> 

<div ng-controller="SimpleController"> 
    <ul> 
     <li ng-repeat="custName in customers | filter: name | orderBy:name">{{ custName.name }} - {{ custName.city | uppercase}} - {{ custName.address }}</li> 
    </ul> 
</div> 
</div> 

我Contorller

function SimpleController($scope){ 

     $scope.customers = [ 

      { 
       name:'John Doe', 
       city:'Aurora', 
       address:'Dickenson' 
      }, 
      { 
       name:'Daniel Pervaiz', 
       city:'Denver', 
       address:'Country Lane' 
      }, 
      { 
       name:'Arooj Paul', 
       city:'Jesu', 
       address:'Green Valley' 
      } 

     ]; 

    } 
+0

http://stackoverflow.com/questions/206540​​59/function-inside-the-angularjs-controller – fdsa

+0

如何SimpleController連接到您的應用程序? RTFM或從離子「標籤」入手的例子開始:您將得到一個好的結構和正確的語法來引用。 –

+0

我使用工作代碼更新了我的答案。我想你沒有正確定義你的ng-app或控制器。除非你沒有發佈你的所有代碼。 – Enkode

回答

0

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

 
myApp.controller('SimpleController', ['$scope', function($scope) { 
 
     $scope.customers = [ 
 
      { 
 
       name:'John Doe', 
 
       city:'Aurora', 
 
       address:'Dickenson' 
 
      }, 
 
      { 
 
       name:'Daniel Pervaiz', 
 
       city:'Denver', 
 
       address:'Country Lane' 
 
      }, 
 
      { 
 
       name:'Arooj Paul', 
 
       city:'Jesu', 
 
       address:'Green Valley' 
 
      } 
 

 
     ]; 
 
    
 
}]);
<!doctype html> 
 
<html lang="en"> 
 
<head> 
 
    <meta charset="UTF-8"> 
 
    <title>Example - example-example8-production</title> 
 

 
    <script src="//ajax.googleapis.com/ajax/libs/angularjs/1.5.5/angular.min.js"></script> 
 
    
 
</head> 
 
<body ng-app="myApp"> 
 

 
    name: <input type="text" ng-model="name"> 
 

 
<div ng-controller="SimpleController"> 
 
    <ul> 
 
     <li ng-repeat="custName in customers | filter: name | orderBy:name">{{ custName.name }} - {{ custName.city | uppercase}} - {{ custName.address }}</li> 
 
    </ul> 
 
</div> 
 

 
</body> 
 
</html>

不知道您的控制器正確定義。

AngularJS controller and methods

還要確保NG-應用適當聲明。

<div ng-app="myApp"> 
0

Angular依賴於模塊的概念。請確保您的應用程序的結構是這樣的:

app.js

angular.module('the_name_of_my_app', [ 
    'ionic', 
    'ngMaterial', 
    'the_name_of_my_app.controllers', 
    'the_name_of_my_app.services', 
    'the_name_of_my_app.directives' 
] 
) 

.run(function(/* Inject whatever is needed */) { 

} 
.config.run(function(/* Inject any Provider, and routes definitions */) { 

}); 

controllers.js

angular.module('the_name_of_my_app.controllers', []) 

.controller('SimpleController', [ 
    '$scope', 
    '$rootScope', // another dependency injection example 
    '$mdDialog', // another dependency injection example 
    '$timeout',// another dependency injection example 
    function 
    (
    $scope, 
    $rootScope, // another dependency injection example 
    $mdDialog, // another dependency injection example 
    $timeout // another dependency injection example 
    ) 
    { 
... 

    }]) 

.controller('AnotherController', [ // ... 

的index.html

您還需要參考您的應用程序在你的看法:

<div ng-app="the_name_of_my_app"> 
0

如果您沒有註冊您的控制器,它會看起來像這樣的角度1.4.9:

angular.js:12722 Error: [ng:areq] Argument 'SimpleController' is not a function, got undefined 

這對角1.0.8:

angular.js:5930 Error: Argument 'SimpleController' is not a function, got undefined 

事實上,你的錯誤說:SimpleController()告訴我你在你的問題中的代碼可能不會對你的錯誤負責(或者使用Angular的一個版本,我還沒有嘗試過 - 我嘗試在plnkr.co上使用最新的和最新的版本)。

爲了解決這個問題,你應該:

  • 清除瀏覽器緩存。

  • 確保您的ng-app(或您用於自舉的任何東西)具有指定的正確主模塊 - 看起來您因爲某些原因沒有出現問題?

  • 確保您正在查找的源目錄與提供靜態內容的服務器的目錄匹配。

  • 搜索源目錄中的SimpleController所有實例 - 它看起來可能是使用ng-controller指令視圖的範圍之外,由於()(或不同版本的角度有不同格式的消息)。

相關問題