title: AngularJS - ngModel cannot be assigned to function
body: <p><br/>
  My question is very common one. I saw many different cases of people with similar question to mine, but I still does not have my answer.</p>
  
  <p><b>[My goals]</b><br/>
  I want to implement a standalone directive to encapsulate div consisting of label(span) and value(input). There are some specifics that worth mentioning here:</p>
  
  <ol>
  <li>My directive aims to preserve the value of the "input" into my page controller score which is an ancestor of the scope my directive works with. Hence with the proper amount of ".$parent" I can access the desired scope for my ngModel assignment.<br/></li>
  <li>My directive does not know the name of the placeholder it shall use to preserve the user input as. This placeholder is coming as an HTML property to my directive.</li>
  </ol>
  
  <p><b>[My approach]</b><br/>
  I am using the directive compile phase to manipulate the directive's HTML (mainly adding properties to directive's template HTML). In the pre-link phase I want manipulate the scope that directive is using in order to add the directive's placeholder to my desired scope and assign it some default value if needed. (I guess I can achieve that with ng-init instead of messing with the scopes, but i believe that is irrelevant to my question).</p>
  
  <p><b>[What I did]</b><br/>
  As I explained above I have a function (determinePlaceholderName()) that returns a string which is the exact value I want to assign to ngModel. For example:<br/></p>
  
  <pre><code>$parent.$parent.$parent.placeHolderName
  </code></pre>
  
  <p>I want to assign that function to input's ng-model attribute assuming that Angular will treath it as an expression, evaluate it and produce what I expected. Here is how I assign the ng-model property:</p>
  
  <pre><code>compile: function(cElem, cAttrs) {
      //Some non relevant code here ....
  
      $(cElem[0]).find("div input").attr("ng-model", "determinePlaceholderName()");
  }
  </code></pre>
  
  <p>Basically what I expect is this to be treated like:</p>
  
  <pre><code>$(cElem[0]).find("div input").attr("ng-model", "$parent.$parent.$parent.placeHolderName");
  </code></pre>
  
  <p><b>[About determinePlaceholderName()]</b><br/>
  This function I define in the link function of my directive:</p>
  
  <pre><code>link: function ($scope) {
  
          $scope.constructValuePlaceholderName = function() {
              //Some logic here.
  
              return result;
  }
  </code></pre>
  
  <p><b>[My Problem]</b><br/>
  And finally what my problem is.
  Angular result in an error explaining to me that</p>
  
  <pre><code>&lt;input ng-model="myFunc()"&gt;
  </code></pre>
  
  <p>is non-assignable expression as documented.</p>
  
  <p><b>[My Question]</b><br/>
  How can I dinamically set ngModel if function assignment is forbidden by Angular??</p>
  
  <p>Thank you for your time!</p>