3

I have a function and i did it in this way

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>

But i am getting the error as

updateInstitution is not defined

Can anyone please suggest help.Thanks.

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');    
            }   
        });     
    } 
    }
  }
}());

But i am getting the error as

updateInstitution is not defined

Can anyone please suggest help.Thanks.

But i am getting the error as

updateInstitution is not defined

Can anyone please suggest help.Thanks.

7
  • Have you defined the "controller as" definition then only can use in this way? Commented Nov 22, 2016 at 9:19
  • What about the context of the function definition? Could you maybe create a fiddle with your current state? Commented Nov 22, 2016 at 9:24
  • var updateInstitution = function(isValid) { /* your functions body */ } Commented Nov 22, 2016 at 9:26
  • Can you post your whole directive structure? Commented Nov 22, 2016 at 9:27
  • Have you defined $scope.vm = vm;? Commented Nov 22, 2016 at 9:28

3 Answers 3

5

You should declare the following in your controller:

var vm = this;
vm.updateInstitution = updateInstitution;
Sign up to request clarification or add additional context in comments.

3 Comments

Can you post the whole code of your directive, or recreate the situation in a fiddle ? This is not enough info for us.
I also see an extra }. Maybe you're closing your controller already before defining the bindables.
No prob, hope we could help.
0

you use $scope.updateInstitution instead of function updateInstitution() , because communication bridge between html and controller is $scope, or for use vm.function you defined $scope.vm

1 Comment

but here i am declaring function in js
-1
 $scope.updateInstitution = function(){
     //Your code
  }

1 Comment

do you see the ng-click as vm.updateInstitution how this will call the function ?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.