表格提交角度指令

时间:2014-07-21 12:29:05

标签: javascript angularjs angularjs-directive angularjs-scope angularjs-ng-repeat

我有一个自定义指令(contentData),用于在我的应用程序中创建自定义表单。哪个有模板文件。此模板文件包含多个数据,由JOSN文件控制。

我试图通过点击提交按钮从数据发送到我的控制器,但我能够做到。

[http://plnkr.co/edit/aB221u18ccNMHfJbOFDo?p=preview][1]

1 个答案:

答案 0 :(得分:1)

你需要在指令定义中添加scope:false,这样当指令内的任何值得到改变时,父范围内都会反映出来。我在plnkr中将范围添加为false,并且$ scope.templateVariable反映在父范围中。

return {
    restrict: 'A',
    scope:false,
    templateUrl :'template.html',
    // NB: no isolated scope!!
    link: function (scope, element, attrs) {
        //console.log(attrs.yourname);
        // executes the expression on the current scope returning the result
        // and adds it to the scope
        scope.variable = scope.$eval(attrs.yourDirective);
        console.log(scope.variable);
       // scope.myfn=function(){
        //  scope.templateVariable='hello world';
        //}
    },
    // the variable is available in directive controller,
    // and can be fetched as done in link function
    controller: ['$scope', '$element', '$attrs',
        function ($scope, $element, $attrs) {
            $scope.myfn = function(){
             $scope.templateVariable='hello world';
            //  $scope.templateVariable = $scope.infogain;
            };
            // executes the expression on the current scope returning the result
            // and adds it to the scope

            //console.log($scope.variable);
        }
     ]
};

working plnkr