nicholas nicholas - 1 year ago 85
AngularJS Question

Shouldn't there be an AngularJS ngWith directive?

Maybe I'm crazy, or too used to KnockoutJS, but I keep looking for an ngWith directive in the docs to define the scope on an element, in a controller, or for an included (ngInclude) partial.

For example:

I'd like to write a controller that augments MyItem like:

MyModule.controller('MyItemCtrl', function($scope) {
$scope.doSomethingToItem = function() {
$ = "bar";

Or a view/template for MyItem like:

<div ng-controller="MyItemCtrl">
<button ng-click="doSomethingWithItem()">Do Something</button>

But in both of these cases I'm imagining my $scope to be prototypically inherit from my model,

But the scope doesn't inherit from the model!!

Which baffles me.

Instead, my model is a property on the scope.

In the case of a repeater:

<div ng-repeat="item in list">
<div ng-controller="MyItemCtrl">
<button ng-click="doSomethingWithItem()">Do Something</button>

which means everywhere I have to use
instead of just
. I have to remember which functions are native to the model, and which were applied directly to the scope by a controller.

If I want to have a partial to display names (stupid example, I know, but you get the idea):


I have to write it


and then ensure the model is always item. Usually by wrapping it in a directive simply to defines a scope with an

What I often feel like I want to do is simply:

<div ng-include="'my/partial.html'" ng-with="item"></div>


<div ng-repeat="list" ng-controller="MyItemCtrl">
<button ng-click="doSomethingWithItem()">Do Something</button>

Is there some magical directive out there that I haven't found? Or am I completely wrong and just looking for trouble?



Many thanks to Brandon Tilley for explaining the dangers of using scopes as models. But I still often find the need for some quick declarative scope manipulation and wish for an ng-with directive.

Take, for example, you have a list of items which, when clicked, shows an expanded view of the selected item. You might write it something like:

<li ng-repeat="item in items" ng-click="selection = item">{{item.minView}}</li>
<div ng-controller="ItemController">

now you have to get properties of the selected item using
rather than what I'd want:
. I'd also have to use
! Making it wholly coupled with this view.

I know, in this simple example I could have a wrapping controller to make it work, but it illustrates the point.

I've written a very basic

myApp.directive('with', ['$parse', '$log', function(parse, log) {

return {
scope: true,
link: function(scope, el, attr) {
var expression = attr.with;
var parts = expression.split(' as ');

if(parts.length != 2) {
log.error("`with` directive expects expression in the form `String as String`");

scope.$watch(parts[0], function(value) {
scope[parts[1]] = value;
}, true);


that simply creates a new scope parsing one expression onto another value, allowing:

<li ng-repeat="item in items" ng-click="selection = item">{{item.minView}}</li>
<div with="selection as item" ng-controller="ItemController">

This seems infinitely useful to me.

Am I missing something here? Just making trouble for myself down the line somehow?

Answer Source

This is a great question. I can see how this may be confusing coming from another front-end framework, but in Angular, the scope has a reference to the model, and the syntax you're describing is normal. I personally like to describe the scope as more like a view model.

Miško Hevery, the author of AngularJS, does a good job of explaining this concept in this video, starting at about the 30 minute mark and lasting about 3 minutes:

People oftentimes think that the scope is the model, and that's not the case. Scope has references to the model. [...] So in the view, you say model dot whatever property you want to access.

While it may be possible to write an ngWith directive that does kind-of what you're looking for, since Angular uses prototypal inheritance for scopes, you will likely run into the same issues that Miško describes in the aforementioned video at 31:10 (where you think you're updating a value on the parent scope but you're actually not). For more details on prototypal inheritance in AngularJS, check out the excellent article The Nuances of Scope Prototypal Inheritance on the AngularJS wiki.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download