0

I used Angularjs $http and $scope to get data from sql server and display it successfully in scopes with no problem. But when I tried to display http json result from separated function in the same scope nothing happened.

All I wanted to know. If I want to get json object in a parameter and select slice from it before displaying, should I make http request every time to do so or can I separate scope away from http request in independent function as shown below.

<%--Angular--%>
<script src="../Scripts/angular.min.js"></script>
<script>

    var jsonObj;
    var app = angular.module('myApp', []);

    app.controller('myCtrl', function ($scope, $http) {
        $http.get('../C_Angular.asmx/ShowSlider')
        .then(function (response) {
            jsonObj = response.data; // this part work well and retrieve data as expected by tracing C_Angular.asmx code
        });
            Select_From_Json = function (x, y) { 
                $scope.Categories = jsonObj.slice(x, y); // but this part don't work when I press Button1 to call Select_From_Json 
            };
    });
</script>

When I press Button1 to call Select_From_Json fuction no displaying take place.

    <input id="Button1" type="button" value="button" onclick="Select_From_Json(2, 4)" />

    <div data-ng-app="myApp" data-ng-controller="myCtrl">

        <div data-ng-repeat="Cat in Categories">

            <div style="background-image: url('{{Cat.Cat_Pic}}');">
                <span>{{Cat.Cat_Name}}</span>
            </div>

        </div>

    </div>
2
  • 1
    Select_From_Json() is not set in $scope. To access it in the html set function $scope.Select_From_Json, And move ng-app above the input Commented May 11, 2017 at 14:15
  • 1
    plus you need to use ng-click instead of onclick as @deblaton-jean-philippe pointed out Commented May 11, 2017 at 14:16

1 Answer 1

3

Your button is outside the controllers scope. That is why the Select_From_Json is not executing properly. Move the button inside the div where the controller is defined.

<div data-ng-app="myApp" data-ng-controller="myCtrl">
 <input id="Button1" type="button" value="button" onclick="Select_From_Json(2, 4)" />

    <div data-ng-repeat="Cat in Categories">

        <div style="background-image: url('{{Cat.Cat_Pic}}');">
            <span>{{Cat.Cat_Name}}</span>
        </div>

    </div>

</div>

You also should be using ng-click for executing functions from a controller like so:

    <div data-ng-app="myApp" data-ng-controller="myCtrl">
 <input id="Button1" type="button" value="button" ng-click="select_From_Json(2, 4)" />

    <div data-ng-repeat="Cat in Categories">

        <div style="background-image: url('{{Cat.Cat_Pic}}');">
            <span>{{Cat.Cat_Name}}</span>
        </div>

    </div>

</div>

And in the controller itself:

$scope.select_From_Json = function(x, y){
    $scope.Categories = jsonObj.slice(x, y);
}
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.