Git Product home page Git Product logo

angular-360-no-scope's Introduction

angular-360-no-scope

What?

$watch your controllerAs controller's data, without injecting $scope.

How do I use it?

  • [npm|bower] install angular-360-no-scope
  • Include angular-360-no-scope.js in your app
  • Add a dependency on angular-360-no-scope to your app module.
  • Write your controller as usual, but avoid $scope
  • Utilize this.$watch() as needed

Live Demo

http://plnkr.co/edit/WzJCeB4CiEpkFyF81Zez?p=preview

Why?

Preface

When using angular's controllerAs, a controller is given a name and a reference to the controller is placed on the $scope. When writing the logic for the controller, data is typically stored directly on the controller itself, not on the $scope. When referencing the data from a template, the data is namespaced by the controllerAs name.

This provides various benefits, which help us write cleaner, more maintainable code. See the style guides by Todd Motto and John Papa for more details.

Still need $scope capabilities

One oddity of writing ControllerAs code is that we no longer tend to have the $scope reference handy. Besides being a tempting dumping ground for data, $scope also provides some important funtionality that we occasionally need such as $watch (and $on, $broadcast, and $emit).

A simple mechanism to provide those functions is to inject $scope into the controller function. Then, in order to watch your controller data, you may do something like so:

$scope.$watch(function() { return ctrl.someData }, callback)

This is a little clunkier than watching scope data, i.e., $scope.$watch("some.scope.variable", callback). Additionally, if you want to watch nested attributes whose parents may or may not be initialized, we might need to add yet another dependency on $parse so we must do something like:

$scope.$watch(function() { return $parse("some.controller.variable")(ctrl); }, callback);

360-no-scope makes this easier

With 360-no-scope, your controllers are decorated, and augmented with a $watch function. The $watch function is bound to the controller instance. This allows you to write ctrl.$watch("some.controller.variable", callback), much like the simple $scope.$watch you are already familiar with.

Sample Controller

app.controller("MyController", function () { // HERE, no $scope is necessary
  var ctrl = this;
  ctrl.watchCount = 0;
  ctrl.foo = {};
  
  // Here is the "scope-less" watch registration.  Watching "ctrl.foo.bar.baz"
  ctrl.$watch("foo.bar.baz", callback);  // <-- HERE, $watch something on the controller
  function callback (newVal, oldVal) { console.log("WatchCount: " + ctrl.watchCount++, newVal, oldVal); }
}

How does it work?

This lib decorates $controllerProvider.register and the $controller service. When a controller is registered with $controllerProvider, or when a controller is instantiated with the $controller() service, the controller fn passed in is augmented with a $watch function (as well as with $on, $broadcast, and $emit).

360-no-scope augments the controller fn by wrapping it in a surrogate controller which is executed instead. The surrogate is annotated with the same injectable dependencies as the real controller fn. Then, $scope is added to the dependency list. When angular instantiates the controller surrogate, the surrogate always gets $scope. It then builds the $scope passthrough functions and adds them to the real controller's prototype. Finally, it instantiates and returns the real controller.

angular-360-no-scope's People

Contributors

askmike avatar christopherthielen avatar mchmielarski avatar patrickjs avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

angular-360-no-scope's Issues

Kudos for the name

Not really an issue but i just wanted to say that you came up with a funny name ;)

Issue if constructor param is array and not function

The documentation defines the register method of the $controllerProvider as method which accepts as constructor parameter either:

  • A function (which your code works)
  • An array containing the dependencies.

In my case the parameter looks like that:
['dep1', 'dep2', 'dep3', function() { }]

In that case the prototype-property is undefined.

is not using explicit annotation and cannot be invoked in strict mode

this module can be really useful, but i've got this error

is not using explicit annotation and cannot be invoked in strict mode

i'm using webpack with this

and have ng-strict-di sit in body tag

if i remove ng-strict-di, 360-no-scope can work.

how can i make this module work without remove ng-strict-di

After uglifying with Grunt controller prototype is undefined

I have next problem:
I do to all my JS files uglifying with Grunt. before uglifying I have to run ng-annotate, but I think there is no issue with it.
When I do this the controller prototype in angular-360-no-scope becomes undefined on injection.
The problem still remains if I do not uglifying the file it self. However, if the code is not uglified all is worked prefect!
I broke my head in order to solve this problem.
Do anybody have solution?
untitled

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.