0

I am trying to pass a data value from one page to another using Angular. I am new to Angular so please bear with me if it sounds silly. Here are my HTML and Angular code. Basically, I want ng-model "jobtitle" value to be filled in the form which is on different page once the user click on that div.

HTML for Page1 (data source)

<div id="portfoliolist" ng-app="careerApp" ng-controller="JobList">
<div class="portfolio DataScience col-xs-12 col-sm-12 col-md-4 col-lg-4" data-cat="DataScience">
<a href="~/career-form.cshtml" class="portfolio-wrapper">
                            <!-- Item Details -->
                            <div class="protfolio-caption-activeWrap">
                                <!-- Centered Details -->
                                <div class="center-details">
                                    <div class="details">
                                        <!-- Item Name -->
                                        <h2 class="name" ng-model="jobtitle">
                                            Winter
                                        </h2>
                                        <p><b>Job ID#</b> <span class="jobid" ng-model="jobid">2017-01</span></p>
                                        <p ng-click="set(data)" class="btn btn-sm">Apply</p>
                                        <!-- Tags -->
                                    </div>
                                </div>
                                <!-- End Center Details Div -->
                            </div>
                            <!-- End Item Details -->
                        </a>
                    </div>
</div>

HTML for Page2 (where Data is to be copied)

<div ng-app="careerApp" ng-controller="JobSelection">
                                    <form class="tsf-step-content">
                                        <label>{{jobtitle}}</label>
                                        <label>{{jobid}}</label>
                                        </form>
</div>

Angular script

    var careerApp = angular.module("careerApp", []);
careerApp.factory('myService', function () {
    var savedData = {}
    function set(data) {
        savedData = data;
    }
    function get() {
        return savedData;
    }

    return {
        set: set,
        get: get
    }

});

careerApp.controller("JobList", ['$scope', function ($scope) {
    myService.set($scope.jobtitle);
}]);
careerApp.controller("JobSelection", ['$scope',function ($scope) {
    $scope.jobtitle = myService.get();
}]);
5
  • You are specifying the ng-app in both html, it must be added just once per App. Commented Jan 2, 2017 at 13:08
  • there are many ways to accomplish this, one way to share data btw pages is to use a service. Commented Jan 2, 2017 at 13:09
  • I removed from the 2nd page. still not working Commented Jan 2, 2017 at 13:10
  • @StenMuchow that's what I have done. I have created a service myService Commented Jan 2, 2017 at 13:11
  • ng-app should wrap the entire app a good place to put it is the html tag the second page is probably not be included as part of the angular app. Commented Jan 2, 2017 at 13:26

2 Answers 2

0

You are wrong in declaring ng-app twice in two different pages. the simpler way to pass data is to store it in $rootScope.

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

1 Comment

Simplier indeed, but extremely polluting to the $rootScope... def not the desired way to handle data.
0

Try something like this

//Controller foo

app.controller('fooController', ['$scope', 'foobarService', function($scope, foobarService){
$scope.StoreId = function(id){
    foobarService.setId(id);
}]);

//foobarService
app.service('foobarService', [function(){
    var id;

    this.setId = function(id){
        id = id;
    }

    this.getId = function(){
        return id;
    }
}]);

//Controller bar
app.controller('barController', ['$scope', 'foobarService', function($scope, foobarService){
    $scope.id = foobarService.getId();
}]);

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.