I have angular controller and Javascript function in that function , i am calling angular function. I am getting error: $scope.Name is not a function, $scope.dates is not a function.

我有角控制器和Javascript函数在那个函数中,我叫角函数。我得到了错误:$scope。Name不是函数,$scope。日期不是一个函数。

     function validation() {
            $scope.pageload = true;

            $scope.Name();
            $scope.dates();  
        }

        $scope.Name = function () {
           // do something
        }

        $scope.dates = function () {
          // do something       
        }

working fine inside the controller

在控制器内正常工作。

    var MyController = function ($scope, service)
    {


       function validation() {

            $scope.pageload = true;

            $scope.Name();
         $scope.dates();

        }

       $scope.Name = function () {


            // do something
        }

     $scope.dates = function () {

            // do something

    }


});


working:


var MyController = function ($scope, service)
{
 LoginHomeService.getHomeService(function (data) {
                $rootScope.CT1SessionObj = data.CT1SessionObj;

                        validation();



                                    }, function (response) {
                                        alert(response.Message);
                                    });

   function validation() {

        $scope.pageload = true;

        $scope.Name();
     $scope.dates();

    }

   $scope.Name = function () {


        // do something
    }

 $scope.dates = function () {

        // do something




});




Not working:

    var MyController = function ($scope, service)
    {
     LoginHomeService.getHomeService(function (data) {
                    $rootScope.CT1SessionObj = data.CT1SessionObj;

                            validation();


   function validation() {

        $scope.pageload = true;

         $scope.Name();
         $scope.dates();

        }

       $scope.Name = function () {


            // do something
        }

     $scope.dates = function () {

            // do something

    }


   }, function (response) {
    alert(response.Message);
   });


   });

3 个解决方案

#1


3

Declare $scope.Name and $scope.dates on top of validation()

宣布美元范围。名称和美元范围。验证日期()

Javascript works from top to bottom, so your functions $scope.Name and $scope.Dates do not exist 'yet'.

Javascript从上到下工作,所以函数值为$scope。名称和美元范围。日期还不存在。

Also, try not to use 'Name' as a function. Most of these words are reserved keywords.

另外,尽量不要使用“Name”作为函数。这些词大部分是保留的关键字。

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

//myApp.directive('myDirective', function() {});
//myApp.factory('myService', function() {});

function MyCtrl($scope) {

    $scope.Name = function() {
    // do something
  }

  $scope.dates = function() {
    // do something       
  }

  function validation() {
    $scope.pageload = true;

    $scope.Name();
    $scope.dates();
  }
}

Fiddle: http://jsfiddle.net/Lvc0u55v/4872/

小提琴:http://jsfiddle.net/Lvc0u55v/4872/

An even better approach would be the 'John Papa style' : Y033

一个更好的方法是“约翰·帕帕帕式”:Y033

Place bindable members at the top of the controller, alphabetized, and not spread through the controller code.

将可绑定的成员放在控制器的顶部,按字母顺序排列,而不是通过控制器代码进行传播。

Why?: Placing bindable members at the top makes it easy to read and helps you instantly identify which members of the controller can be bound and used in the View.

为什么?:将可绑定的成员放在顶部可以方便地阅读,并帮助您立即识别哪些控制器的成员可以被绑定并在视图中使用。

Why?: Setting anonymous functions in-line can be easy, but when those functions are more than 1 line of code they can reduce the readability. Defining the functions below the bindable members (the functions will be hoisted) moves the implementation details down, keeps the bindable members up top, and makes it easier to read.

为什么?:在行中设置匿名函数很容易,但是当这些函数超过一行代码时,就会降低可读性。定义可绑定成员下面的函数(函数将被提升)将实现细节向下移动,将可绑定的成员保持在顶部,并使其更易于阅读。

/* avoid */
function SessionsController() {
    var vm = this;

    vm.gotoSession = function() {
      /* ... */
    };
    vm.refresh = function() {
      /* ... */
    };
    vm.search = function() {
      /* ... */
    };
    vm.sessions = [];
    vm.title = 'Sessions';
}


/* recommended */
function SessionsController() {
    var vm = this;

    vm.gotoSession = gotoSession;
    vm.refresh = refresh;
    vm.search = search;
    vm.sessions = [];
    vm.title = 'Sessions';

    ////////////

    function gotoSession() {
      /* */
    }

    function refresh() {
      /* */
    }

    function search() {
      /* */
    }
}

更多相关文章

  1. 使用HTML文件中的React调用.js文件中的Javascript函数
  2. 在传递给google.setOnLoadCallback()的函数中使用参数;
  3. 另一个iframe中的iframe的onload函数
  4. 使用Object.observe 实现数据绑定
  5. JavaScript 中的函数介绍
  6. ABP(现代ASP.NET样板开发框架)系列之21、ABP展现层——Javascrip
  7. 为什么括号用于包装javascript函数调用? [重复]
  8. javascript 构造函数中的属性与原型上属性优先级的比较
  9. 在javascript中调用带有双参数()的函数

随机推荐

  1. 移动商城项目总结
  2. pgsql 修改字段类型为json
  3. Android:shape设置corners(圆角)不能正常显
  4. Elasticsearch就是这么简单
  5. NacosSync多集群迁移
  6. 递归就这么简单
  7. 学了这么久java反射,你知道class.forName
  8. 华为HMS Core音频服务,让有声世界更动听
  9. Springboot2.x整合异步任务
  10. MyBatis之Mapper XML 文件详解(三)-Resul