0

i have this controller and this view (html). if i give an initial value to $scope.article.Context, it will be shown in the view. (means that the view recognizes the controller). when i change the $scope.article.Context value inside a controller function, i dont see it changing in the view. any idea ? Thanks !!!

controller:

angular.module('generalApp', [])
    .controller('menuController', function ($scope, $http) {

        $scope.article = {Context:"initial value"}  ;

        $scope.menuLinkSelected = function (articleId) {
            $http.post("Home/GetArticle?articleId=" + articleId).then(function (response) {
                $scope.article.Context = "new value";
                $scope.$apply();
            });
        };

html:

<body ng-app="generalApp">

<div ng-controller="menuController">
    <div ng-include="'partials/topMenu.html'"></div>
    <div ng-include="'partials/sideMenu.html'"></div>

    <div ng-model="article">
        <label>{{article.Context}}</label>
    </div>

    <input type="button" value="click" onclick="ziba()" />
</div>

3
  • You do not need the $scope.apply(). What you have should work. You do not need to set the ngModel on the div. What you have on the label should suffice. Like @Jake mentioned, are you sure your $http call is returning? Commented Apr 21, 2014 at 13:52
  • @moshi, did either of these answers end up working for you? Commented Apr 21, 2014 at 17:52
  • Hi. OK, I removed the $scope.$apply. About the $http call, yes, it returns value, but guys, look at this row - $scope.article.Context = "new value"; I even send a hard coded value. Even if there where no return from $http, i do return a value here.... Commented Apr 21, 2014 at 18:35

2 Answers 2

1

Are you sure that your $http POST request is actually returning? You could test this by changing your controller to update $scope.article.Context like this:

var timerFn = function(){
  $scope.article.Context = "It works!";
};

$timeout(timerFn, 1000);

If that's the issue, you may want to try using this $http syntax to handle your POST request's callback:

$http.post('someUrl')
     .success(function(data, status){
       console.log(data);
     })
     .error(function(data, status){
       console.log(data);
     });

Sidenotes:

(1) You can probably just set your ng-model in the DOM to "article.Context" although what you have should work.

(2) You don't need to call $scope.$apply() inside of Angular. It's only used to update bindings when changes occur outside of Angular.

Sign up to request clarification or add additional context in comments.

2 Comments

Hi, yes , the $scope.$apply() doesn't have to be there - my mistake. About the data coming or not, so it comes, since I put an alert to verify. About the "article.Context", it doesn't do any change... any other idea ? Thanks !!!
Have you tried using another property name besides "Context"? As you can see from the blue formatting above, it appears to be a reserved keyword. (Although I've never heard of it or used it.) Could you try $scope.article.test123 instead of $scope.article.Context?
1

Solution:

<div>
   <label ng-bind="article.Context"></label>
</div>

You're ng-model doesn't do anything when you assign it to that div.

JSFiddle :

Here you have a working jsfiddle using your example.

2 Comments

Hi, about writing ng-model inside the div, it should be ok, i have it working somewhere else like this. I just don't know what is the problem here... btw - article.Context won't solve it :-( any other idea ? Thanks !!!
@moshi, hi. If you see my fiddle, your example works :) Maybe there are something else in your controller or view that causes it to fail.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.