0

I need to bind a variable with a JS object's property. Is it possible ?

In my controller

...
$scope.data = "initial value";
$scope.obj = {data : $scope.data}
...

And in my View

<input type="text" ng-model="data">
<h1> {{ obj.data }} </h1>

When i update the text in my input box it should update the value in <h1> and it should be done through the object.

No functions, No watchers or anything, Just with binding. Is it possible ?

This is my question simplified. what im trying to do is create a object with some changing variables, and pass it to an AngularJS component where it will display the value changes. I need to pass an object, not different values individually.

1
  • 3
    have you tried ng-model="obj.data" ? Commented Aug 24, 2017 at 4:24

2 Answers 2

1

bind your object with ng-model

.............
$scope.obj = {data : "initial value"}
................

your view

<input type="text" ng-model="obj.data">
<h1> {{ obj.data }} </h1>
Sign up to request clarification or add additional context in comments.

Comments

1

You should use <input type="text" ng-model="obj.data">

DEMO

var app = angular.module('testApp',[]);
app.controller('testCtrl',function($scope){
   $scope.data = "initial value";
   $scope.obj = {data : $scope.data}
});
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<body ng-app="testApp" ng-controller="testCtrl">
<input type="text" ng-model="obj.data">
<h1> {{ obj.data }} </h1>
</body>

1 Comment

delete the line <input type="text" ng-model="obj.data">, and manipulate obj.data through javascript function. will that work?

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.