0

there are buttons in detail.html file:

<div ng-controller="test.views.detail">
<div data-ng-repeat="item in details"  scroll>
    <button ng-click="showDetails(item)">The details</button>

in detail.js file

angular.module('test')
.controller('test.views.detail', function($scope) {

    $scope.detailsClicked = false;

    $scope.showDetails = function(item){
        $scope.detailsClicked = true;
    }....

in formDetail.html code:

<div ng-controller="test.views.detail">
{{detailsClicked}}
<div ng-if="detailsClicked">...

Initially it shows false for detailsClicked, when I click on button it goes to showDetails function but value of $scope.detailsClicked never get updated! It is straight forward not sure why it doesn't work:(

12
  • 1
    I think it is because there is an additional scope introduced with ng-repeat. Commented Feb 9, 2015 at 0:11
  • 1
    If you use the ControllerAs syntax instead you should be able to get away with it but to be honest I haven't done Angular in a little while so am a little rusty (but I still remembered this so...). I think the ControllerAs will allow you to specifically refer to the controllers detailsClicked property and so angular will know what to do (I think) =P. Commented Feb 9, 2015 at 0:15
  • 2
    How does formDetail.html come into play? Commented Feb 9, 2015 at 0:16
  • 1
    Have you tried moving $scope.detailsClicked to $scope.obj = { detailsClicked: false};? It's best practice to 'always have a dot in bindings' in earlier versions of angular. Commented Feb 9, 2015 at 0:17
  • 2
    There are two instances of the test.views.detail controller - one in formDetail.html and the other in detail.html. If you want to share state betweeen the two instances, create a service that'll hold the state or use state params if you're using ui-router. Commented Feb 9, 2015 at 0:59

1 Answer 1

3

This is because you're using the same controller at two places and expecting the scope object to be the same which it is not. Everytime you call ng-controller in your markup a new scope object will be created. If you want them to be based off the same data then use a service.

Here is an example

app.controller('test.views.detail', function($scope, detailsClicked) {

    $scope.detailsClicked = detailsClicked;

    $scope.showDetails = function(item){
        $scope.detailsClicked.isClicked = true;
    }
});

Create a factory/service which will retain the data, make sure the data is a

app.factory('detailsClicked', function(){
  var data = {
    isClicked: false
  }

  return data;
});
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.