mbrc
mbrc

Reputation: 3953

Angular scope.$eval returns back undefined. Can I check if expression exist in scope?

.directive('aaValMsgFor', ['aaFormExtensions', 'aaUtils', function (aaFormExtensions, aaUtils) {
          //generate the validation message for a particular form field here
          return {
              require: ['^form'],
              priority: 1,
              scope: true,
              link: function ($scope, element, attrs) {

                  var fullFieldPath = attrs.aaValMsgFor;
                  var fieldInForm, formObj;

                  var innerScope = $scope;
                  while ((!fieldInForm || !formObj) && innerScope) {
                      fieldInForm = innerScope.$eval(fullFieldPath);
                      formObj = innerScope.$eval(fullFieldPath.substring(0, fullFieldPath.indexOf('.')));

                      if ((!fieldInForm || !formObj)) {
                          innerScope = innerScope.$parent;
                      }
                  }

innerScope.$eval(fullFieldPath); I get always back undefined.

How can I check if my fullFieldPath which is model.name exist in scope? Can I check all expressions in scope so that I know If I am in a correct way?

EDIT:

I also do before:

$compile(element)(scope);

and element has name: model.name

but later on innerScope.$eval does not work ...

Upvotes: 0

Views: 196

Answers (1)

rrd
rrd

Reputation: 5957

You can change your link function to include the model if you supply the controller argument to the directive (it's injected as link's fourth arg):

controller: 'SomeController',
controllerAs: 'vm',
require: ['^form'],
link: function($scope, element, attrs, controller) {
  // Change controller.name here to see if it exists;
  if (controller.name) {

  }
}

Upvotes: 2

Related Questions