2014-09-10 67 views
3

我試圖訪問我的指令內部的表單以進行驗證,所以我想訪問$ setPristine,但是,我似乎無法弄清楚如何使用templateUrl創建表單。AngularJS - 我如何訪問我的指令中的templateUrl中定義的表單?

我有一個plunker詳細說明這裏的問題:http://plnkr.co/edit/Sp53xzdTbYxL6DAue1uV?p=preview

我得到一個錯誤:

Controller 'form', required by directive 'testDirective', can't be found! 

下面是相關Plunker代碼:

的.js:

var app = angular.module("myApp", []); 

app.directive("testDirective", function() { 
    return { 
    restrict: 'E', 
    scope: {}, 
    templateUrl: "formTemplate.html", 
    require: "^form", // <-- doesn't work 
    link: function (scope, element, attrs, ctrl) { 
     console.log(ctrl); 

     scope.open = function() { 
      // Would like to have access to the form here 
      // ctrl.$setPristine(); 
     } 
    }, 
    controller: function($scope) { 
     $scope.firstName = "Mark"; 

     $scope.save = function(form) { 
     console.log(form); 
     } 
    } 
    } 
}) 

formTemplate.html:

<form name="testForm" ng-click="save(testForm)"> 
    <input type="text" ng-model="firstName" /> 
    <br> 
    <input type="submit" value="Save" /> 
</form> 

如何將formTemplate.html中的表單附加到我的指令的隔離範圍中?

回答

相關問題