Pass form to directive

前端 未结 5 2130
盖世英雄少女心
盖世英雄少女心 2020-11-30 19:35

I want to encapsulate my form fields in a directive so I can simply do this:

5条回答
  •  广开言路
    2020-11-30 19:53

    Made your 'What I'd prefer' fiddle work! For some reason you could see the "$scope.ngForm" string in a console.log, but logging it directly didn't work, resulting in undefined. However, you can get it if you pass attributes to the controller function.

    app.directive('myForm', function() {
    return {
        restrict: 'A',
        controller: ['$scope','$element','$attrs', function($scope,$element,$attrs) {
            this.getForm = function() {
                return $scope[$attrs['ngForm']];
            }
        }]
    }
    });
    

    http://jsfiddle.net/vZ6MD/20/

提交回复
热议问题