Simple AngularJS Form is undefined in Scope

前端 未结 7 1834
独厮守ぢ
独厮守ぢ 2020-12-13 03:33

I\'m starting to play around with AngularJS forms in jsfiddle and I\'ve come across a problem already where a very simple form example is not working as expected. All I have

7条回答
  •  情深已故
    2020-12-13 04:12

    I wrote a directive to deal with this issue. Its an attribute that you can put on your form and pass a function to which will execute when the form is ready.

    Javascript

    angular.module('app').directive('formInit', function(){
      return {
        restrict: 'A',
        scope: {
          init: '&formInit'
        },
        controller: function($scope, $element){
          var name = null;
          if ($element[0] && $element[0].name){
            name = $element[0].name;
          }
    
          var listener = $scope.$watch('init', function(){
            if ($scope[name] && $scope.init){
              $scope.init();
              listener();
            }
          });
        }
      };
    });
    

    Example HTML

    Where testing is a function on your controllers scope.

提交回复
热议问题