在同一个Controller Angular Js中将函数调用到另一个函数中

时间:2015-06-05 09:54:31

标签: angularjs function

我是使用angularjs的新手,我在控制器中声明了两个函数,现在我想将一个函数用于另一个函数我该怎么做 意味着如果我将函数名称称为另一个函数,则表示未定义。

这是代码:

'use strict';
angular.module('customer').controller('Controller', ['$scope', '$state', 'Sservice',
  function($scope, $state, Sservice) {

    var that = this;

    (function getDetails() {
        //IMPLEMENTATION
    }());

    this.function2 = function function2 (id){
        //implementation
      getDetails(); // says undefined
    };
  }
]);

8 个答案:

答案 0 :(得分:12)

.controller('SampleController',function($scope){
$scope.funcA = function(){
   $scope.funcB();//scope level function
   funcC(); //non scope level function``
}
$scope.funcB = function(){
}
var funcC = function(){
}
});

答案 1 :(得分:3)

最适合我

var app = angular.module('MyApp', []);
app.controller('MyCtrl',['$scope',function($scope)
 {
$scope.functionA=function(){
alert("Inside functionA")
$scope.functionB(); 	
}; 								 
$scope.functionB=function(){ 	 									
alert("Inside functionB");	 
}
}]);
<!DOCTYPE html>
<html ng-app="MyApp" ng-controller="MyCtrl">
  
<head>
  <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.9/angular.min.js"></script>
<meta charset="ISO-8859-1">
<title>Insert title here</title>
</head>
<body>


<input type="button" value="Click to call functionA" ng-click="functionA()">


</body>
</html>

答案 2 :(得分:2)

.controller('SampleController',function($scope){
$scope.funcA = function(){
   $scope.funcB();//scope level function
   funcC(); //non scope level function``
}
$scope.funcB = function(){
}
var funcC = function(){
}
});

答案 3 :(得分:0)

我不知道你想要达到的目的是什么,但你可以简单地将你的两个函数声明为

function getDetails() {
    //IMPLEMENTATION
}

this.function2 = function(id) {
    getDetails(); 
};

答案 4 :(得分:0)

你让事情变得复杂。简单地说,这样做

'use strict';
angular.module('customer').controller('Controller', ['$scope', '$state', 'Sservice',
      function($scope, $state, Sservice) {

        function getDetails() {
            //IMPLEMENTATION
        };

        function function2 (id){
            //implementation
          getDetails(); // says undefined
        };
      }
]);

答案 5 :(得分:0)

上面的示例中混淆了几个代码区域。首先,function2未正确声明。

您已将getDetails功能包裹在所谓的self-executing anonymous function中。这意味着SEAF包装器外部的代码不可见,包括function2。省略SEAF包装器,以便在getDetails想要使用它时定义function2

最后,您正在使用Angular,但在控制器上将function2分配给this。这可能不是你想要做的;您要向HTML公开的功能应附加到$scope,而不是this

'use strict';
angular.module('customer').controller('Controller', ['$scope', '$state', 'Sservice',
  function($scope, $state, Sservice) {

    function getDetails() {
        //IMPLEMENTATION
    }

    $scope.function2 = function(id) {
        //implementation
        getDetails();
    };
  }
]);

答案 6 :(得分:0)

My these options below could help

'use strict';
angular.module('customer').controller('Controller', ['$scope', '$state', 'Sservice',
  function($scope, $state, Sservice) {

    function getDetails() {
        //IMPLEMENTATION
    };

    function function2 (id){
        //implementation
      getDetails(); // says undefined
    };
  }
]);


or 

'use strict';
angular.module('customer').controller('Controller', ['$scope', '$state', 'Sservice',
  function($scope, $state, Sservice) {

    $scope.getDetails = function() {
        //IMPLEMENTATION
    };

    $scope.function2 = function(id){
        //implementation
      $scope.getDetails(); // says undefined
    };
  }
]);

答案 7 :(得分:0)

对我来说工作正常:

{
    // define angular module/app

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

    // create angular controller and pass in $scope and $http
    function formController($scope, $http) {

        $scope.sitelist = function(){
            $http.get("http://mars.ourgoogle.in/clients/techinfini/customcms/index.php/Ajax/sitelist").then(function(items){    
            console.log(items.data);
            $scope.list = items.data;       
        }); 
    }

    // process the form
    $scope.processForm = function() {
        $http({
            method  : 'POST',
            url     : 'http://mars.ourgoogle.in/clients/techinfini/customcms/index.php/Ajax/angulartest',
            data    : $.param($scope.formData),  // pass in data as strings
            headers : { 'Content-Type': 'application/x-www-form-urlencoded' }  // set the headers so angular passing info as form data (not request payload)
        }).success(function(data) {
                $scope.sitelist();
           }
        }
    }
}