0

I am having trouble to access angularjs expressions after the click event.

index.html

<div class="row">
    <a href="#/company"><div class="tile col col-comp cat-a-g grow-appear" ng-click="onSelect(item)" ng-repeat="item in data" >
        <p>{{item.compname}}</p>
    </div></a>
</div>

The above code populate a list of company names. When I click on a company it opens company.html

company.html

<div class="comp-info col s12 grow-appear">
   <span class="comp-logo-container">
      <img src="images/aap3-160x70-black.jpg" />
   </span>
   <span class="comp-info-container">
      <p><i class="far fa-envelope"></i>{{item.compemail}}</p>
      <p><i class="fas fa-location-arrow"></i>{{item.compbuilding}}</p>
      <p><i class="fas fa-phone"></i>{{item.compphone}}</p>
   </span>
</div>

Controller (app.js)

$scope.onSelect = function($event, data) {
   console.log(data);
}

File Structure

root
    +--public
        +--templates
            +--company.html
            +--index.html
        +--javascript
            +--app.js
    +--views
        +main.html

I am using ngRoute to inject index.html and company.html into main.html.

app.config(function($routeProvider){
    $routeProvider

    .when ('/', {
         templateUrl: 'templates/home.html',
         controller: 'AppCtrl'
    })

    .when ('/company', {
         templateUrl: 'templates/company.html',
         controller: 'AppCtrl'
    })
});

So when I console.log() the controller, it shows the array of data but it doesn't add it to the company html document. What am I missing?

1 Answer 1

0

The structure of your application is a little unclear, but I think your problem may be to do with $scope. item in item in data is not accessible from the root of $scope. You can do something like this:

<div class="comp-info col s12 grow-appear">
   <span class="comp-logo-container">
       <img src="images/aap3-160x70-black.jpg" />
   </span>
   <span class="comp-info-container">
       <p><i class="far fa-envelope"></i>{{selected.compemail}}</p>
       <p><i class="fas fa-location-arrow"></i>{{selected.compbuilding}}
       </p>
       <p><i class="fas fa-phone"></i>{{selected.compphone}}</p>
   </span>
</div>

Then:

$scope.onSelect = function($event, data) {
  $scope.selected = data;
}  
Sign up to request clarification or add additional context in comments.

9 Comments

It's not the file structure I am referring to but the binding of your controllers to the templates. It is not clear how either index.html or company.html is bound the controller that provides its scope. You need an ng-controller directive or some kind of routing set-up to link the views to their respective controllers. How are you doing that?
I am using ngRoute to link the views to the controller. They share the same controller.
OK, so when you change states, you will lose your old scope and a new one will be created. You need to pass the identifier of the item as a parameter. See e.g. stackoverflow.com/questions/30003582/…
One (not recommended) way to get the result you want without making any major modifications to your setup would be to use $rootScope to store the selected item. $rootScope.selected = data. Not recommended because it's dirty - you should minimise use of $rootScope - but at least you can see it working.
So I understand that somehow it needs to know when I click on a link which item I am trying to retrieve. But I don't understand that if I pass the indentifier of the item as a parameter how can I access the rest of the value of the item (e.g. item.compname, item.compemail ...)
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.