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

1

1 Answer 1

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

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.