creating a new variable in directive scope -- [Question Asked]

Query asked by user

I am trying to create a brand new scope variable in the directive. It just consists of simplified version of my JSON data. I want to avoid processing complex JSON objects in the HTML hence I am trying to create a JSON data structure known only to directive.

I want to know HOW can we create a brand new variable ?

I tried something like this and Angularjs throws a bunch of errors in the console. I don’t know exactly what is it complaining about.

angularApp.directive('viewPersonData', function() {
    return {
        restrict: 'E',
        replace: true,
        scope: {
          addressData: '=',
          professionalData: '=',
          creditData: '=',
         },
       templateUrl: 'viewPersonData.html',
      link: linker
    };

function linker(scope, element, attrs) {

 scope.showData= function() {
                if( something) 
               { 

              //defining a new scope variable
              scope.employees = [];

                var firstname = "John";
                var lastname = "Smith";
                var employee = {
                    "firstname": firstname,
                    "lastname": lastname
                }

                scope.employees.push(employee);

                 return true;
              }

        }
}

});

Even though HTML template ( viewPersonData.html ) does retrieve the employee data through below code.

<tbody ng-repeat="employee in employees">
                <tr>
                    <td class="alnLt b" >{{employee.firstname}}</td>

                    <td class="alnLt b">{{employee.lastname}}</td>
                </tr>
                </tbody>

I see following errors in the console :

Error: 10 $digest() iterations reached. Aborting!
Watchers fired in the last 5 iterations: [["fn: $watchCollectionWatch; newVal: 8; oldVal: 7","fn: function (context) {\r\n          try {\r\n            for(var i = 0, ii = length, part; i<ii; i++) {\r\n              if (typeof (part = parts[i]) == 'function') {\r\n                part = part(context);\r\n                if (part == null || part == undefined) {\r\n                  part = '';\r\n                } else if (typeof part != 'string') {\r\n                  part = toJson(part);\r\n                }\r\n              }\r\n              concat[i] = part;\r\n            }\r\n            return concat.join('');\r\n          }\r\n          catch(err) {\r\n            var newErr = new Error('Error while interpolating: ' + text + '\\n' + err.toString());\r\n            $exceptionHandler(newErr);\r\n          }\r\n        }; newVal: \"John\"; oldVal: undefined","fn: function (context) {\r\n          try {\r\n            for(var i = 0, ii = length, part; i<ii; i++) {\r\n              if (typeof (part = parts[i]) == 'function') {\r\n                part = part(context);\r\n                if (part == null || part == undefined) {\r\n                  part = '';\r\n                } else if (typeof part != 'string') {\r\n                  part = toJson(part);\r\n                }\r\n              }\r\n              concat[i] = part;\r\n            }\r\n            return concat.join('');\r\n          }\r\n          catch(err) {\r\n            var newErr = new Error('Error while interpolating: ' + text + '\\n' + err.toString());\r\n            $exceptionHandler(newErr);\r\n          }\r\n        }; newVal: \"Smith\"; oldVal: undefined"],["fn: $watchCollectionWatch; newVal: 9; oldVal: 8","fn: function (context) {\r\n          try {\r\n            for(var i = 0, ii = length, part; i<ii; i++) {\r\n              if (typeof (part = parts[i]) == 'function') {\r\n                part = part(context);\r\n                if (part == null || part == undefined) {\r\n                  part = '';\r\n                } else if (typeof part != 'string') {\r\n                  part = toJson(part);\r\n                }\r\n              }\r\n              concat[i] = part;\r\n            }\r\n            return concat.join('');\r\n          }\r\n          catch(err) {\r\n            var newErr = new Error('Error while interpolating: ' + text + '\\n' + err.toString());\r\n            $exceptionHandler(newErr);\r\n          }\r\n        }; newVal: \"John\"; oldVal: undefined","fn: function (context) {\r\n          try {\r\n            for(var i = 0, ii = length, part; i<ii; i++) {\r\n              if (typeof (part = parts[i]) == 'function') {\r\n                part = part(context);\r\n                if (part == null || part == undefined) {\r\n                  part = '';\r\n                } else if (typeof part != 'string') {\r\n                  part = toJson(part);\r\n                }\r\n              }\r\n              concat[i] = part;\r\n            }\r\n            return concat.join('');\r\n          }\r\n          catch(err) {\r\n            var newErr = new Error('Error while interpolating: ' + text + '\\n' + err.toString());\r\n            $exceptionHandler(newErr);\r\n          }\r\n        }; newVal: \"Smith\"; oldVal: undefined"],["fn: $watchCollectionWatch; newVal: 10; oldVal: 9","fn: function (context) {\r\n          try {\r\n            for(var i = 0, ii = length, part; i<ii; i++) {\r\n              if (typeof (part = parts[i]) == 'function') {\r\n                part = part(context);\r\n                if (part == null || part == undefined) {\r\n                  part = '';\r\n                } else if (typeof part != 'string') {\r\n                  part = toJson(part);\r\n                }\r\n              }\r\n              concat[i] = part;\r\n            }\r\n            return concat.join('');\r\n          }\r\n          catch(err) {\r\n            var newErr = new Error('Error while interpolating: ' + text + '\\n' + err.toString());\r\n            $exceptionHandler(newErr);\r\n          }\r\n        }; newVal: \"John\"; oldVal: undefined","fn: function (context) {\r\n          try {\r\n            for(var i = 0, ii = length, part; i<ii; i++) {\r\n              if (typeof (part = parts[i]) == 'function') {\r\n                part = part(context);\r\n                if (part == null || part == undefined) {\r\n                  part = '';\r\n                } else if (typeof part != 'string') {\r\n                  part = toJson(part);\r\n                }\r\n              }\r\n              concat[i] = part;\r\n            }\r\n            return concat.join('');\r\n          }\r\n          catch(err) {\r\n            var newErr = new Error('Error while interpolating: ' + text + '\\n' + err.toString());\r\n            $exceptionHandler(newErr);\r\n          }\r\n        }; newVal: \"Smith\"; oldVal: undefined"],["fn: $watchCollectionWatch; newVal: 11; oldVal: 10","fn: function (context) {\r\n          try {\r\n            for(var i = 0, ii = length, part; i<ii; i++) {\r\n              if (typeof (part = parts[i]) == 'function') {\r\n                part = part(context);\r\n                if (part == null || part == undefined) {\r\n                  part = '';\r\n                } else if (typeof part != 'string') {\r\n                  part = toJson(part);\r\n                }\r\n              }\r\n              concat[i] = part;\r\n            }\r\n            return concat.join('');\r\n          }\r\n          catch(err) {\r\n            var newErr = new Error('Error while interpolating: ' + text + '\\n' + err.toString());\r\n            $exceptionHandler(newErr);\r\n          }\r\n        }; newVal: \"John\"; oldVal: undefined","fn: function (context) {\r\n          try {\r\n            for(var i = 0, ii = length, part; i<ii; i++) {\r\n              if (typeof (part = parts[i]) == 'function') {\r\n                part = part(context);\r\n                if (part == null || part == undefined) {\r\n                  part = '';\r\n                } else if (typeof part != 'string') {\r\n                  part = toJson(part);\r\n                }\r\n              }\r\n              concat[i] = part;\r\n            }\r\n            return concat.join('');\r\n          }\r\n          catch(err) {\r\n            var newErr = new Error('Error while interpolating: ' + text + '\\n' + err.toString());\r\n            $exceptionHandler(newErr);\r\n          }\r\n        }; newVal: \"Smith\"; oldVal: undefined"],["fn: $watchCollectionWatch; newVal: 12; oldVal: 11","fn: function (context) {\r\n          try {\r\n            for(var i = 0, ii = length, part; i<ii; i++) {\r\n              if (typeof (part = parts[i]) == 'function') {\r\n                part = part(context);\r\n                if (part == null || part == undefined) {\r\n                  part = '';\r\n                } else if (typeof part != 'string') {\r\n                  part = toJson(part);\r\n                }\r\n              }\r\n              concat[i] = part;\r\n            }\r\n            return concat.join('');\r\n          }\r\n          catch(err) {\r\n            var newErr = new Error('Error while interpolating: ' + text + '\\n' + err.toString());\r\n            $exceptionHandler(newErr);\r\n          }\r\n        }; newVal: \"John\"; oldVal: undefined","fn: function (context) {\r\n          try {\r\n            for(var i = 0, ii = length, part; i<ii; i++) {\r\n              if (typeof (part = parts[i]) == 'function') {\r\n                part = part(context);\r\n                if (part == null || part == undefined) {\r\n                  part = '';\r\n                } else if (typeof part != 'string') {\r\n                  part = toJson(part);\r\n                }\r\n              }\r\n              concat[i] = part;\r\n            }\r\n            return concat.join('');\r\n          }\r\n          catch(err) {\r\n            var newErr = new Error('Error while interpolating: ' + text + '\\n' + err.toString());\r\n            $exceptionHandler(newErr);\r\n          }\r\n        }; newVal: \"Smith\"; oldVal: undefined"]]
    at Error (native)
    at Object.Scope.$digest (angular.js:8841:61)
    at Object.Scope.$apply (angular.js:9012:24)
    at done (angular.js:10265:45)
    at completeRequest (angular.js:10449:7)
    at XMLHttpRequest.xhr.onreadystatechange (angular.js:10404:11) 

Solution

Get rid of the linker and use the controller attribute of the directive,

controller: (['$scope', function($scope) {
    $scope.employees = [{
      "firstname": firstname,
      "lastname": lastname
    }];
  }
])

This creates the employees data structure.
Pass the function inside the array. Otherwise, if minifier is used, webpack will replace $scope by some alphabet. Then it won’t work in the production.

If not passed inside the array, you’ll get the error Error Reference/ $injector/ unpr in the production environment.

Answered By – Carlos Pliego

This Answer collected from stackoverflow, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0


What is Angular?

Angular is an open-source, JavaScript outline written in TypeScript. Google keeps up with it, and its basic role is to foster single-page activities. As an edge, Angular enjoys clear benefits while likewise outfitting a standard design for formulators to work with. It empowers stoners to deliver huge tasks in a viable way. textures overall lift web improvement viability and execution by outfitting an agreeable construction so that formulators do n't need to continue to modify regulation from scratch. textures are efficient devices that offer formulators a large group of extra elements that can be added to programming easily.

However, is JavaScript ideal for creating single-sprinter activities that bear particularity, testability, and trend-setter efficiency? maybe not.

JavaScript is the most by and large utilized client-side prearranging language. It's composed into HTML reports to empower relations with web sprinters in endless extraordinary ways. As a genuinely simple to-learn language with inescapable help, creating current operations is appropriate.

Nowadays, we have various textures and libraries intended to give essential outcomes. As for front end web advancement, Angular addresses incalculable, while possibly not all, of the issues formulators face while utilizing JavaScript all alone.
Who we are?

We are team of software engineers in multiple domains like Programming and coding, Fundamentals of computer science, Design and architecture, Algorithms and data structures, Information analysis, Debugging software and Testing software. We are working on Systems developer and application developer. We are curious, methodical, rational, analytical, and logical. Some of us are also conventional, meaning we're conscientious and conservative.

Answer collected from stackoverflow and other sources, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0