0

The basic premise is that I'm trying to access the element's value attribute after it compiles and copy it into a variable. For example: if vm.datasource.data['+ i +'].HolidayName compiled into "Christmas" I want to grab that value and store it in a variable.

Is it possible to copy the compiled value into a variable?

var link = function(scope, elem, attrs){

      setTimeout(function(){
        var checkboxCellArray = elem.find('.md-checkbox-cell');

        _.forOwn(checkboxCellArray, function(node, i) {
          if(angular.element(node).hasClass('md-checkbox-cell')){
            var element = angular.element('<p value="{{vm.selected.length}} checked. {{vm.headers[0].orderBy}}. {{vm.datasource.data['+ i +'].HolidayName}}. {{vm.headers[1].orderBy}}. {{vm.datasource.data['+ i +'].Country}}"></p>')
            var compiled = $compile(element)(scope);

            setTimeout(function(){
              console.log(compiled);
            }, 500)
          }
        });
      }, 1000)
    }
Darien Lombardi
  • 105
  • 1
  • 7

2 Answers2

0

Seems after hours of toiling i've found the solution 5 minutes after posting...

simply add scope.$apply() after compiling and the variable will have the updated values.

Darien Lombardi
  • 105
  • 1
  • 7
0

The real problem was you were using setTimeout function and updating stuff of angular from it. setTimeout asynchronous event which is modifying angular scope from from outside angular world doesn't intimate angular digest system to run digest cycle. You have to kick off it manually to update binding over an HTML page. This problem can be easily fix by using using $timeout instead of setTimeout, which run your function in setTimeout & will run digest cycle after it.

Code

$timeout(function(){
    var checkboxCellArray = elem.find('.md-checkbox-cell');

    _.forOwn(checkboxCellArray, function(node, i) {
      if(angular.element(node).hasClass('md-checkbox-cell')){
        var element = angular.element('<p value="{{vm.selected.length}} checked. {{vm.headers[0].orderBy}}. {{vm.datasource.data['+ i +'].HolidayName}}. {{vm.headers[1].orderBy}}. {{vm.datasource.data['+ i +'].Country}}"></p>')
        var compiled = $compile(element)(scope);

        $timeout(function(){
          console.log(compiled);
        }, 500)
      }
    });
  }, 1000)
}
Pankaj Parkar
  • 134,766
  • 23
  • 234
  • 299