Here I have a hard-coded html form name (in a save function) in an angular controller. Instead, I want to get the name of the form in a variable and put it there. I mentioned similar questions , but could not find a solution.
HTML form
<section xmlns="http://www.w3.org/1999/html">
<div class="page-header">
<h1>{{vm.drug._id ? 'Edit Drug Information' : 'Add New Drug'}}</h1>
</div>
<div class="col-md-12">
<form name="vm.form.addNewDrugForm" class="form-horizontal" ng-submit="vm.save(vm.form.addNewDrugForm.$valid)"
novalidate>
</form>
</div>
</section>
Angular Controller
(function () {
'use strict';
angular
.module('drugs')
.controller('DrugsController', DrugsController);
DrugsController.$inject = ['$scope', '$state', '$window', 'Authentication', 'drugResolve'];
function DrugsController($scope, $state, $window, Authentication, drug) {
var vm = this;
vm.authentication = Authentication;
vm.drug = drug;
vm.error = null;
vm.form = {};
vm.remove = remove;
vm.save = save;
vm.printDrug = printDrug;
function save(isValid) {
if (!isValid) {
$scope.$broadcast('show-errors-check-validity', 'vm.form.addNewDrugForm');
return false;
}
if (vm.drug._id) {
vm.drug.$update(successCallback, errorCallback);
} else {
vm.drug.$save(successCallback, errorCallback);
}
function successCallback(res) {
$state.go('drugs.view', {
drugId: res._id
});
}
function errorCallback(res) {
vm.error = res.data.message;
}
}
}
}());
source
share