1

How to call Json array value in other controller. Basically i work with nested controller. Nested controller will return first character of firstname and lastname.

For example if my first name is Anand Jee then It will return AJ, Rohan Kumar then it will return RK.

My AngularJS code.

//create module
var myApp = angular.module("myApp", []);

//create controller

myApp.controller("empCtrl", function ($scope) {
    var Employees = [
        { FirstName: "Anand", LastName: "Jee", Roles: "Web Developer", Technology: ".NET" },
        { FirstName: "Pritus", LastName: "Dubey", Roles: "Window App Developer", Technology: "XAMARIN" },
        { FirstName: "Vineet", LastName: "Rai", Roles: "Web Developer", Technology: ".NET" },
        { FirstName: "Nilesh", LastName: "Pathak", Roles: "UI/UX Developer", Technology: "Photoshop" },
        { FirstName: "Vikesh", LastName: "Juyal", Roles: "Web Developer", Technology: "PHP" }
    ];
    $scope.Employees = Employees;
});

myApp.controller("EmpShortName", function ($scope) {
    $scope.getEmpShortName = function () {

        //$scope.empShortName  = $scope.Employees.FirstName + " " + $scope.Employees.LastName;//here is problem
        $scope.empShortName = "NP";//For temp declaration
        return $scope.empShortName;
    };
});

PLUNKER DEMO

ANJYR
  • 2,583
  • 6
  • 39
  • 60
  • 1
    Possible duplicate of [angularjs Access parent scope from child controller](http://stackoverflow.com/questions/21453697/angularjs-access-parent-scope-from-child-controller) – Patrick Evans Jan 08 '16 at 17:32

1 Answers1

1

Pass the Employee object into the getEmpShortName function. Then just return the parsed value. Also, no need for the EmpShortName controller, just put the scope function in your empCtrl controller:

HTML:

<div class="empTextImage">{{getEmpShortName(emp)}}</div>

JS:

$scope.getEmpShortName = function (emp) {
    return emp.FirstName.substr(0,1) + emp.LastName.substr(0,1);
};

Updated Plunker

Corey
  • 5,818
  • 2
  • 24
  • 37