1
angular.module('myApp', []).controller('namesCtrl', function($scope) {
$scope.names = [
    {name:'Jani',country:'Norway'},
    {name:'Hege',country:'Sweden'},
    {name:'Kai',country:'Denmark'}
];
});

function abc(){
 $.ajax({url: "demo_test.txt", success: function(result){
     $scope.names = result; // here
 }});
}

I want to change the content of scope.names by result(it will be in json format) and then render the contents through angular.

And i want to clarify that i am bound to use get request from inside a function, so cant use $http of angular

6
  • Don't use $.ajax, use $http Commented Jun 5, 2015 at 16:45
  • @entre Cant use $http Commented Jun 5, 2015 at 16:46
  • This might help. Commented Jun 5, 2015 at 16:50
  • What's the reason you can't use $http Commented Jun 5, 2015 at 16:51
  • @SahilJain could you look at mine answer Commented Jun 5, 2015 at 17:00

3 Answers 3

1

Use $http instead of ajax because $http takes care of running digest cycle which takes care of 2 way binding.

function abc(){
  $http.get("demo_test.txt")
  .then(function(result){
     $scope.names = result;
  });
}

If for whatever reason you cannot use $http then you have to call $scope.$apply method to set the scoped values because it internally runs the digest cycle.

function abc(){
 $.ajax({
    url: "demo_test.txt", 
    success: function(result){
      $scope.$apply(function () {
         $scope.names = result;
      });
    }
 });
}

If you have abc method outside of your controller then you will have to get the controller scope as shown below which however is not a recommended by angular.

function abc(){
 var $scope = angular.element('[ng-controller=namesCtrl]').scope();
 $.ajax({
    url: "demo_test.txt", 
    success: function(result){
      $scope.$apply(function () {
         $scope.names = result;
      });
    }
 });
}
Sign up to request clarification or add additional context in comments.

8 Comments

And how does abc knows about $scope?
OP is already accessing $scope inside abc so I assume that it is in a closure.
@OP - how are you calling abc method?
I am sure what OP meant by $scope.names = result; // here is how will he access the scope outside the controller.
@ShankarSangoli Good copy paste.. :p I already have angular.element('[ng-controller=namesCtrl]').scope() thing in my answer..
|
0

I understand you don't have access to the scope. You need to access scope in hacky way by using angular.element('[ng-controller=namesCtrl]').scope() then you would have actual control over scope, but the variable changes in a scope wouldn't run digest cycle, You need to run it manually by using $apply() on that scope.

Code

function abc(){
 $.ajax({url: "demo_test.txt", success: function(result){
    var scope = angular.element('[ng-controller=namesCtrl]').scope()
    scope.names = result; // here
    if(!scope.$$phase)
      scope.$apply(); //this will make available names in namesCtrl scope.
 }});
}

Comments

0

You need something to let Angular know that it needs to watch for that change. The Angular way of doing this is with $http, but you can also use $scope.$watch.

1 Comment

Actually, i am a newbie to angular. So it wud be helpful if you can provide me a snippet

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.