AngularJS: require with bindToController, required controller is not available? -
i have directive user require , bindtocontroller in definition.
(function(){ 'use strict'; angular.module('core') .directive('formvalidationmanager', formvalidationmanager); function formvalidationmanager() { return { require: { formctrl: 'form' }, restrict: 'a', controller: 'formvalidationmanagerctrl fvmctrl', priority: -1, bindtocontroller: true }; } }());
according angular docs:
if require property object , bindtocontroller truthy, required controllers bound controller using keys of require property. binding occurs after controllers have been constructed before $oninit called. see $compileprovider helper example of how can used.
so expect in controller:
(function () { 'use strict'; angular .module('core') .controller('formvalidationmanagerctrl', formvalidationmanagerctrl); formvalidationmanagerctrl.$inject = ['$timeout', '$scope']; function formvalidationmanagerctrl($timeout, $scope) { var vm = this; vm.api = { validate: validate }; //vm.$oninit = activate; function activate() { } function validate() { $scope.$broadcast('fvm.validating'); var firstinvalidfield = true; object.keys(vm.formctrl).foreach(function (key) { var prop = vm.formctrl[key]; if (prop && prop.hasownproperty('$settouched') && prop.$invalid) { prop.$settouched(); if (firstinvalidfield) { $timeout(function(){ var el = $('[name="' + prop.$name + '"]')[0]; el.scrollintoview(); }, 0); firstinvalidfield = false; } } }); return firstinvalidfield; } } })();
vm.formctrl populated form controller. however, undefined. why undefined? additionally, tried access in $oninit function $oninit function never got called. doing wrong?
i using directive so:
<form novalidate name="edituserform" form-validation-manager>
i'm not sure if issue, think directive declaration should this:
controller: 'formvalidationmanagerctrl', controlleras: 'vm',
rather than:
controller: 'formvalidationmanagerctrl fvmctrl',
Comments
Post a Comment