0
(function() {

    angular
        .module('myApp.home', [])

        .factory('myService', function($http) {
            return {
                getInfo: function() {
                    // return something
                }
            };
        })

        .controller('HomeController', function($scope, $routeParams, myService) {
                var my = this;      
                var sid;

                myService.getInfo().success(function(data) {

                    my.id   = data.id;
                    //sid= my.id;
                });

                //my.id     = sid;
        });
})();

I'm trying to access the variable id in my view. But I'm not able to do so. I'm getting an undefined variable error.

I even tried to declare a variable sid globally so that it can be accessed from everywhere and I tried assigning value to it as well but that effort also didn't bring up any results.

My services are working fine and they are returning data. I'm trying to use the id in my view but don't know where I messed it up. Any help?

EDIT

And my HTML is like this:

<div data-id="my.id"></div>
6
  • where is vm.id in controller? It should rather by $scope.id = my.id and in html <div>{{id}}</div> Commented May 31, 2016 at 10:45
  • I want to pass it as a data attribute. @Rahul Arora Commented May 31, 2016 at 10:47
  • <div data-id="{{id}}"></div> and $scope.id = my.id Commented May 31, 2016 at 10:51
  • Did you declare controller like this ng-controller='HomeController as vm'? Commented May 31, 2016 at 10:54
  • hmm, we might need more infor of your html :\ Commented May 31, 2016 at 10:57

4 Answers 4

0

If you want to use it in your view you have to bind it to a scope e.g. $scope.id - check this link for reference.

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

4 Comments

I tried this. Even when I use $scope, sometimes it works and most of time, it returns undefined. @SeRu
can you show the code where you are trying it with $scope?
AngularJS only binds that data to the template that is linked with $scope in the controller
You dont call your controller in the html. try HomeController.id instead of vm.id
0

Below is your html, how it should be.

<div ng-app="app" ng-controller="HomeController as home">
     <div data-id="home.id">{{home.id}}</div>
</div>

Below is how your controller part.

angular.module('app', [])

.controller('HomeController', function () {
    this.id = "someValue";    
});

http://jsfiddle.net/grdmLfxt/

3 Comments

Error: [$compile:multidir] Multiple directives [ngController, uibTabset (module: ui.bootstrap.tabs)] asking for new/isolated scope on: <div active="active" ng-controller="HomeController as vm"> @Thalaivar
@AmeliaEarheart: very difficult to say from here, can you share your complete html part... and btw instead of vm.id you can also use $scope.id
I haved added full html. @Thalaivar
0

use controllerAs : vm then you can access vm.id in your view.

HTML

<div ng-app="app" ng-controller="HomeController as vm">
     <div data-id="vm.id">{{vm.id}}</div>
</div>

Controller

.controller('HomeController', function () {
    var vm = this;
    vm.id = "someValue";    
});

for more info read this

Comments

0

small change in javascript code and html. try this.

Javascript

(function() {

    angular
        .module('myApp.home', [])

        .factory('myService', function($http) {
            return {
                getInfo: function() {
                    // return something
                }
            };
        })

        .controller('HomeController', function($scope, $routeParams, myService) {
                var my = this;      
                var sid;

                myService.getInfo().success(function(data) {

                    $scope.id   = data.id;  // changed from my.id to $scope.id
                    //sid= my.id;
                });

                //my.id     = sid;
        });
})();

HTML

<div data-id="id"></div>

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.