40

I am new in AngularJs, ng-click is not working as expected. I searched on the internet , Follow the tutorial , (that was working) - but this is not working!!!

My Code:

<div class="row" ng:repeat="follower in myform.all_followers | partition:2">
    <ons-col class="views-row" size="50" ng-repeat="data in follower" >
        <img ng-src="http://dealsscanner.com/obaidtnc/plugmug/uploads/{{data.token}}/thumbnail/{{data.Path}}" alt="{{data.fname}}" ng-click="showDetail2(data.token)"/>
        <h3 class="title" ng-click="showDetail2('ss')">{{data.fname}}</h3>
    </ons-col>
</div>

Here is my controller

//Follows Controller
app.controller('FollowsController', function($scope, $http) {
    var ukey = window.localStorage.ukey;

    $scope.myform ={};
    $scope.myform.reports ="";
    $http.defaults.headers.post["Content-Type"] = "application/x-www-form-urlencoded";
    var dataObject = "usertoken="+ukey;
    var responsePromise = $http.post(server_url+"follows/", dataObject,{});
    responsePromise.success(function(dataFromServer, status,    headers, config) {
        $scope.myform.all_followers = dataFromServer;
        console.log(dataFromServer);

        //alert(dataFromServer);
        $scope.showDetail = function(index) {
            profileusertoken =  index;
            $scope.ons.navigator.pushPage('profile.html'); 
        }

        function showDetail2(index) {
            alert("here");
        }

        $scope.showDetail2 = showDetail2;
    });
});

Niether showDetail working nor showDetail2 Please help Thanks

Max
  • 915
  • 10
  • 28
  • 4
    Define "not working". – dfsq Aug 21 '14 at 09:21
  • Which element did you bind your controller to? Don't forget `ng-repeat` creates its own scope. – desbo Aug 21 '14 at 09:22
  • Check if `ons-col` directive is not creating its own isolate scope. If that's the case, the `showDetail2` function will not be available. (Quick test, set the `ng-click` on the `
    ` and see if it works)
    – bmleite Aug 21 '14 at 09:27

6 Answers6

12

Just add the function reference to the $scope in the controller:

for example if you want the function MyFunction to work in ng-click just add to the controller:

app.controller("MyController", ["$scope", function($scope) {
   $scope.MyFunction = MyFunction;
}]);
Doron Saar
  • 446
  • 5
  • 7
8

It just happend to me. I solved the problem by tracing backward from the point ng-click is coded. Found out that an extra

</div> 

was placed in the html to prematurely close the div block that contains the ng-click.

Removed the extra

</div> 

then everything is working fine.

Daniel C. Deng
  • 1,573
  • 13
  • 8
5

Have a look at this plunker

HTML:

<!DOCTYPE html>
<html ng-app="app">

  <head>
    <script data-require="angular.js@1.3.0-beta.16" data-semver="1.3.0-beta.16" src="https://code.angularjs.org/1.3.0-beta.16/angular.min.js"></script>
    <link rel="stylesheet" href="style.css" />
    <script src="script.js"></script>
  </head>

  <body ng-controller="FollowsController">
    <div class="row" ng:repeat="follower in myform.all_followers">
      <ons-col class="views-row" size="50" ng-repeat="data in follower">
        <img ng-src="http://dealsscanner.com/obaidtnc/plugmug/uploads/{{data.token}}/thumbnail/{{data.Path}}" alt="{{data.fname}}" ng-click="showDetail2(data.token)" />
        <h3 class="title" ng-click="showDetail2('ss')">{{data.fname}}</h3>
      </ons-col>
    </div>
  </body>

</html>

Javascript:

var app = angular.module('app', []);
//Follows Controller
app.controller('FollowsController', function($scope, $http) {
    var ukey = window.localStorage.ukey;
    //alert(dataFromServer);
    $scope.showDetail = function(index) {
        profileusertoken =  index;
        $scope.ons.navigator.pushPage('profile.html'); 
    }

    function showDetail2(index) {
        alert("here");
    }

    $scope.showDetail2 = showDetail2;
    $scope.myform ={};
    $scope.myform.reports ="";
    $http.defaults.headers.post["Content-Type"] = "application/x-www-form-urlencoded";
    var dataObject = "usertoken="+ukey;
    //var responsePromise = $http.post("follows/", dataObject,{});
    //responsePromise.success(function(dataFromServer, status,    headers, config) {

    $scope.myform.all_followers = [[{fname: "blah"}, {fname: "blah"}, {fname: "blah"}, {fname: "blah"}]];
});
Max
  • 915
  • 10
  • 28
Wawy
  • 6,259
  • 2
  • 23
  • 23
  • I was using ng-controller="MyController as ctrl"... and then ctrl.doSomething(). Removing the ctrl thing and using the method directly, as you did, worked. Thanks. – Vaibhav Jun 12 '16 at 13:49
  • 3
    @Vaibhav The format you were using will only work if you assign the methods to the `this` object of your function and not `$scope` directly. – Wawy Jun 12 '16 at 14:04
3

For ng-click working properly you need define your controller after angularjs script binding and use it via $scope.

bummi
  • 27,123
  • 14
  • 62
  • 101
Kirill Shur
  • 280
  • 2
  • 4
1

i tried using the same ng-click for two elements with same name showDetail2('abc')

it is working for me . can you check rest of the code which may be breaking you to move further.

here is the sample jsfiddle i tried:

Dilip Tirumala
  • 371
  • 1
  • 10
1

My actual component wasn't visible due to *ngIf, and I was clicking on another component and expecting click to work. This can happen if you copy/paste code or work on existing code after long.

Mohammad Zaid Pathan
  • 16,304
  • 7
  • 99
  • 130