javascript - AngularJS — injecting scope (without $) into a controller -


in material design mddialog documentation, i’ve noticed they’ve passed scope (without prefixed dollar sign) dialogcontroller near bottom.

(function(angular, undefined){   "use strict";   angular    .module('demoapp', ['ngmaterial'])    .controller('appctrl', appcontroller);   function appcontroller($scope, $mddialog) {     var alert;     $scope.showalert = showalert;     $scope.showdialog = showdialog;     $scope.items = [1, 2, 3];     // internal method     function showalert() {       alert = $mddialog.alert({         title: 'attention',         content: 'this example of how easy dialogs can be!',         ok: 'close'       });       $mddialog         .show( alert )         .finally(function() {           alert = undefined;         });     }     function showdialog($event) {        var parentel = angular.element(document.body);        $mddialog.show({          parent: parentel,          targetevent: $event,          template:            '<md-dialog aria-label="list dialog">' +            '  <md-dialog-content>'+            '    <md-list>'+            '      <md-list-item ng-repeat="item in items">'+            '       <p>number {{item}}</p>' +            '      '+            '    </md-list-item></md-list>'+            '  </md-dialog-content>' +            '  <div class="md-actions">' +            '    <md-button ng-click="closedialog()" class="md-primary">' +            '      close dialog' +            '    </md-button>' +            '  </div>' +            '</md-dialog>',          locals: {            items: $scope.items          },          controller: dialogcontroller       });       function dialogcontroller(scope, $mddialog, items) {         scope.items = items;         scope.closedialog = function() {           $mddialog.hide();         }       }     } })(angular); 

i've read $ naming convention , way make sure variables don't overwritten. why code failing follow convention? i.e in context, how know when use $ or not, , significance? believe in case must more naming convention, or authors have chosen use $scope purposes of consistency.

note: aware of difference between $scope , scope in linking functions, scope pointing fixed set of parameters. not believe why scope used in context, feel free let me know if wrong.

thanks!

i think docs inconsistent here - while being correct @ same time.

the scope , $scope here same, figured out after reading source code. culprit line inside interimelement locals extended options in turn have scope property.

 return showdone = compilepromise.then(function(compiledata) {               angular.extend(compiledata.locals, self.options);                element = compiledata.link(options.scope); 

i'm pretty sure having $scope accessible scope incident , keep things clean 1 should use $scope refer value provided $injector i.e. in controllers.

i've submitted a pull request fix inconsistency , compiled a pen demonstrating usage.


Comments