如何在angularjs中声明一个函数

时间:2016-11-22 09:17:25

标签: javascript angularjs angular

我有一个功能,我这样做了

JS:

function updateInstitution (isValid) {alert('hi')
    if (!isValid) {
        $scope.$broadcast('show-errors-check-validity', 'vm.form.institutionForm');
        return false;
    }
    var data = JSON.stringify(vm.institution);
    httpService.put('institutions/' + vm.institution_id, data).then(function (results) {
        if (results && results.data && results.data.details) {
            vm.institution = results.data.details;
            formInstitutionData('profile');
            commonService.showNotification('success', 'Institution Details updated successfully!');
            $('#institutionModal').modal('hide');    
        }   
    });     
} 
}
  vm.updateInstitution = updateInstitution;

Html:

<button type="button" class="btn btn-blue"  ng-click="vm.updateInstitution(vm.form.institutionForm.$valid)" ng-bind="field.saveText"></button>

但我收到的错误是

updateInstitution is not defined

任何人都可以建议帮助。谢谢。

JS:

    (function () {
  'use strict';

  // Institutions controller
  angular
          .module('institutions')
          .controller('InstitutionsController', InstitutionsController);

  InstitutionsController.$inject = ['$scope', '$state', '$window', '$timeout', 'httpService', 'Authentication', 'commonService'];



    function active() {
      httpService.get('institutions/' + vm.institution_id).then(function (results) {
        if (results && results.data && results.data.details) {
          vm.institutionCopyData = angular.copy(results.data.details);
          formInstitutionData('all');
        }
      });
    }

    $scope.editInstitutionModal = function (type) {
      $scope.field = {};
      $scope.showInstitutionModal = false;
      if (type === 'basicedit') {
            $scope.field.field_type = 'edit-institution.form.client';
            $scope.field.formName = 'Edit institution (' + vm.institutionObj.name + ')';
            $scope.field.saveText = 'Update';  
      }
      if(type === 'general'){
            $scope.field.field_type = 'add-general.form.client';
            $scope.field.formName = 'General Info';
            $scope.field.saveText = 'Save';          
      }
      $timeout(function () {
        $scope.showInstitutionModal = true;
        $('#institutionModal').modal('show');
        $scope.$apply();
      }, 10);
    };


    function updateInstitution (isValid) {alert('hi')
        if (!isValid) {
            $scope.$broadcast('show-errors-check-validity', 'vm.form.institutionForm');
            return false;
        }
        var data = JSON.stringify(vm.institution);
        httpService.put('institutions/' + vm.institution_id, data).then(function (results) {
            if (results && results.data && results.data.details) {
                vm.institution = results.data.details;
                formInstitutionData('profile');
                commonService.showNotification('success', 'Institution Details updated successfully!');
                $('#institutionModal').modal('hide');    
            }   
        });     
    } 
    }
  }
}());

但我收到的错误是

updateInstitution is not defined

任何人都可以建议帮助。谢谢。

但我收到的错误是

updateInstitution is not defined

任何人都可以建议帮助。谢谢。

3 个答案:

答案 0 :(得分:5)

您应该在控制器中声明以下内容:

var vm = this;
vm.updateInstitution = updateInstitution;

答案 1 :(得分:0)

您使用 $ scope.updateInstitution 而不是函数updateInstitution(),因为html和控制器之间的通信桥接是$ scope, 或者使用vm.function定义$ scope.vm

答案 2 :(得分:-1)

 $scope.updateInstitution = function(){
     //Your code
  }