0
<div ng-app =“App” ng-controller = “ctrl”>
<input ng-model = “firstName”>
<p ng-bind = “showFirstName”></p>

<script>
var app=(‘App’,[]);
app.controller(‘ctrl’,function($scope)
{
//This Line
$scope.showFirstName = $scope.firstName;
});
</script>

I want to set showFirstName dynamically rather than assigning a constant String. Is it possible with Angular?

0

1 Answer 1

0

Yes, you can do it using factory

//Create App
var app = angular.module('myApp',[]);

//Create a factory
app.factory('DbService', function($http){
    return{
      fetchData : function(parameters){
        return $http.post('/url-to-fetch-data',parameters).then(function (response) {
				      return response.data;
		    });      
      }
    }
});

//inject factory to controller
app.controller('mycontroller', ['$scope', 'DbService', function($scope, DbService){
    $scope.name = '';
    //parameters is a javascript object here you can pass the parameters from client if you want
    var parameters = {id:'125', token:"sd13ds6dsds454dsd4"};
  DbService.fetchData(parameters).then(function(data){
    $scope.name = data.name
  })

}])

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.