Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Value is not binding with scope's variable if used inside uib-tabset

Value is not binding with scope's variable if used inside uib-tabset. Here in following example I tried to get $scope.message inside uib-tab and outside of it :

angular.module("app", ["ui.bootstrap"])
		.controller("myctrlr", ["$scope", function($scope){
            $scope.message = "my message ";
		}]);
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap.min.css" rel="stylesheet"/>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/angular-ui-bootstrap/0.14.3/ui-bootstrap-tpls.min.js"></script>
<div ng-app="app" ng-controller="myctrlr" class="col-md-6 col-md-offset-3">
	<uib-tabset>
	    <uib-tab heading="Static title">
            <input type="text" ng-model="message" ng-change="change()" /> 
            <br />
            Inside uib-tab : {{ message }}
	  	</uib-tab>
	    <uib-tab heading="Another title">
	      I've got an HTML heading, and a select callback. Pretty cool!
	    </uib-tab>
  	</uib-tabset>
    Outside uib-tab : {{ message }}
</div>

I declared $scope.message and tried to bind it with the input element inside uib-tab. But when I changed it's value, the changes are not reflecting on outside of uib-tab.

jsfiddle link

like image 712
afzalex Avatar asked Nov 06 '15 06:11

afzalex


People also ask

Can you change the scope of a variable in a script?

Changes to variables are only affected in that scope unless you use a scope modifier to change the variable’s scope. However, you can incorporate the scope of a script or function by using dot source notation. When the script runs in the current scope, any script variables are available in the current scope.

Is $scope myitem set in ngmodelctrl?

This repository has been archived by the owner. It is now read-only. $scope.myItem never gets set. It's like ngModelCtrl.$setViewValue (newVal) cannot bind to a simple variable $scope.myItem.

How do I incorporate the scope of a script or function?

However, you can incorporate the scope of a script or function by using dot source notation. When the script runs in the current scope, any script variables are available in the current scope. You can use dot source notation by placing a dot/period (.) and a space before the script name. Examine the script file and code below:

What is the difference between outer scope and nested scope?

The outer scope is the parent scope, and any nested scopes are child scopes of that parent. An item is available in the scope where it is defined and to any child scopes unless explicitly made private.


1 Answers

Basically in angular, if you bind to a primitive the value of the variable is passed around, and not the reference to it, which can break 2-way binding. I'm guessing that the tabset directive creates its own scope, so the valueInScope variable defined in the controller loses its binding in the child scope of the tabset because its a primitive.

 $scope.data = {message:'my message'}

Solution by Object

also you can use $parent.parentproperty to bind child scope. This will prevent the child scope from creating its own property.

Solution by using $parent

like image 82
Durgpal Singh Avatar answered Oct 12 '22 03:10

Durgpal Singh