1

This is my first project on angular. I have created a directive. I am not able to insert the value which I am fetching from an object served by a RESTful API. The object as shown in the console is like this,

enter image description here

Below are the necessary files,

knob.js(directive)

angular.module('knob', [])

.directive('knob', function() {
    return {
        restrict: 'A',
        link: function(scope, element, attrs) {
            element.knob({
                fgColor: attrs.fgColor
            });
        }
    }
})  

dashboard.js(controller)

myApp.controller('DashController', function($scope, $http, $cookies, $cookieStore) {

    $http({
        url: site + '/company/dashboard',
        method: "GET",
        transformRequest: encodeurl,
        headers: {
            'Content-Type': 'application/x-www-form-urlencoded'
        }
    }).success(function(data) {
        console.log(data); //data has the object that i want to use
        $scope.dash = data.count;
    }).error(function(res) {
        console.log(res);
    });

});  

dahboard.html(view)

<li>
    <div class="knob_contain">
      <h4 class="knobs_title">Deals Left This Month</h4>
      <input type="text" value="{{dash.profile}}" knob class="dial dialIndex">
    </div>
</li>  

I want the count.profile value to be bound to input type where I am using my knob. I will give you more files if you need.

5
  • create a plunkr if possible Commented Jan 21, 2015 at 8:38
  • 1
    <input type="text" ng-model="dash.profile" knob class="dial dialIndex"> Commented Jan 21, 2015 at 8:45
  • It returns me nothing but blank. Commented Jan 21, 2015 at 8:54
  • please create a plunker with some hardcorded sample data Commented Jan 21, 2015 at 8:58
  • I am really sorry. It was my mistake. After asking this question. I removed the $scope.dash... stuff from my code to do something else. Thank you for your answer. Works. Can you make it an answer so that I may except it? I would also love to give a property of data-readonly= "true". I would appreciate if you can add that in your answer as well. Upvoted. Commented Jan 21, 2015 at 9:07

3 Answers 3

1

The better way is assign a model to text box like,

 <input type="text" ng-model="dash.profile" knob class="dial dialIndex">
Sign up to request clarification or add additional context in comments.

Comments

1

You can change your code look like

AngulaJs code

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

app.service('yourService', function ($http) {
    this.getUser = function () {
        return $http({ url: site + '/company/dashboard',
            method: "GET",
            transformRequest: encodeurl,
            headers: {
                'Content-Type': 'application/x-www-form-urlencoded'
            }
    })
     .success(function(data) {
        console.log(data); //data has the object that i want to use
        $scope.dash = data;
    }).error(function(res) {
        console.log(res);
    });
    };
});

app.controller('DashController', function($scope, $http, $cookies, $cookieStore, yourService) {
  $scope.dash =null;

    yourService.getUser().then(function (resp) {   
        if (resp.data !== undefined) {           
             console.log(data); //data has the object that i want to use
             $scope.dash = data.count;
        }
      })
      .error(function(res){
           console.log(res);
      });
}); 

app.directive('knob', function() {
    return {
        restrict: 'A',
        link: function(scope, element, attrs) {
            element.knob({
                fgColor: attrs.fgColor
            });
        }
    }
});

HTML code sample

< li>
    <div class="knob_contain">
      <h4 class="knobs_title">Deals Left This Month</h4>
      <input type="text" value="{{dash.profile}}" knob class="dial dialIndex">
    </div>
</li>  

1 Comment

Welcome @ThomasSebastian. I already faced this kind of issue in my current app.
1

You need to make a small change in your dashboard.html page.

For input elements, the ng-model property sets the value of the input box. For details see the docs on input[text]

So instead of using

<input type="text" value="{{dash.profile}}" knob class="dial dialIndex">

use

<input type="text" ng-model="dash.profile" knob class="dial dialIndex">

1 Comment

Ya I had missed this. Corrected. Thanks.

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.