I have a directive with isolated scope as following:
application.directive("myDirective",function(){ return { restrict: "A", scope: {myDirective:"="}, link : function(scope) { console.log("My directive: ",scope.myDirective) // works fine scope.person={name:"John",surname:"Doe"} scope.hello=function(){ console.log("Hello world!") } } } }) Corresponding view:
<div my-directive='{testValue:3}'> Testvalue: {{myDirective}}<br/> Hello {{person}} <button ng-click="hello()">Say hello</button> </div> And it seems that i cannot use any of the fields declared in the scope. In view the "myDirecive" and "person" fields are blank and the scope's "hello" function is not executed when i press the button.
It works fine when i pass scope="true" to the directive but does not work in isolated scope.
Am i missing something here or maybe there is no way to introduce variables to the isolated scope of a directive?
UPDATE
Updated question presenting why i would rather not to use a static template. The effect i am trying to achieve is to make directive that allows to upload any html form getting the form initial data via rest/json. The whole process is rather complex and application specific therefore i cannot use any available form libraries. I present below the simplified version of the use case:
The updated directive
application.directive("myForm",function(){ return { restrict: "A", scope: {myForm:"="}, link : function(scope) { console.log("Form parameters: ",scope.myForm) // works fine scope.formData=... // Get form initial data as JSON from server scope.submitForm=function(){ // Send scope.formData via REST to the server } } } }) The case when i would like to use this form. Of course i would like to use this directive many times with different forms.
<form my-form='{postUrl:'/myPostUrl',getFormDataUrl:'/url/to/some/json}'> <div>Form user: {{formData.userName}} {{formData.userSurname}} <input type="text" ng-model="formData.userAge" /> <input type="text" ng-model="formData.userEmail" /> <button ng-click="submitForm()">Submit</button> </form> I hope this explains why i cannot use one static html template for this scenario.
Maybe someone can explain why this is working with scope="true" and with an isolated scope i cannot access any scoped variables?