Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting the scope within Angular UI tabs

I'm having some trouble with angular scopes within the context of a tab (Angular UI).

Here is my jsfiddle example (slimmed down example showing fields on multiple tabs with a single submit button outside of the tabs).

Here is the basic html layout:

<form...>
  <tab>
    <pane>
      <input...>
    </pane>
  </tab>
</form>

Looking at the jsfiddle example, without the tabs if I submit a very basic form I would see the $scope.user be an object. Since the fields are within panes (tabs) the scope is incorrect. How can I get access to the submitted user object in createUser?

like image 201
Arthur Frankel Avatar asked Apr 01 '13 21:04

Arthur Frankel


People also ask

Is there any scope in angular?

AngularJS Scope The scope is the binding part between the HTML (view) and the JavaScript (controller). The scope is an object with the available properties and methods. The scope is available for both the view and the controller.

What is $scope used for in angular?

The $scope in an AngularJS is a built-in object, which contains application data and methods. You can create properties to a $scope object inside a controller function and assign a value or function to it. The $scope is glue between a controller and view (HTML).

How many types of scopes are available in angular?

There are two types of Scope in AngularJS. The JavaScript function that makes/changes/removes/controls the data known as Controller.

What is scope and $scope in AngularJS?

The $ in "$scope" indicates that the scope value is being injected into the current context. $scope is a service provided by $scopeProvider . You can inject it into controllers, directives or other services using Angular's built-in dependency injector: module.


1 Answers

The problem is that the user model is buried deep within an isolate scope. To get the code working as expected, you would need to alert out the value of $scope.$$childHead.panes[0].$$nextSibling.user.first_name not $scope.user. This is obviously not a solution.

A simple solution is to add an empty user object to the controller scope. The ng-model directives in your HTML will then bind to this property. Your controller should look like this:

function MyCtrl($scope) {
  $scope.user = {};
  $scope.createUser = function () {
    alert($scope.user);
  };
}
like image 133
Noah Freitas Avatar answered Dec 07 '22 00:12

Noah Freitas