angularjs - Angular JS controller not working when enclosed with $(function () {}); -


when enclose controller code within $(function () {}); method, stopped working. please find sample code mentioned bleow:

contacts.cshtml

@{     layout = null; }  <!doctype html> <html> <head>     <meta name="viewport" content="width=device-width" />     <title>contacts</title>      <script type="text/javascript" src="//ajax.googleapis.com/ajax/libs/angularjs/1.0.7/angular.min.js"></script> </head> <body>     <div ng-app="" ng-controller="contactscontroller">         <form>             <label>name</label>             <input type="text" name="name" ng-model="newcontact.name" />             <label>email</label>             <input type="text" name="email" ng-model="newcontact.email" />             <label>phone</label>             <input type="text" name="phone" ng-model="newcontact.phone" />             <br />             <input type="hidden" ng-model="newcontact.id" />             <input type="button" value="save" ng-click="savecontact()" class="btn btn-primary" />         </form>         <table class="table table-striped table-bordered">             <thead>                 <tr>                     <th>name</th>                     <th>email</th>                     <th>phone</th>                     <th>action</th>                 </tr>             </thead>             <tbody>                 <tr ng-repeat="contact in contacts">                     <td>{{contact.name}}</td>                     <td>{{contact.email}}</td>                     <td>{{contact.phone}}</td>                     <td>                         <a href="#" ng-click="edit(contact.id)">edit</a>                         <a href="#" ng-click="delete(contact.id)">delete</a>                     </td>                 </tr>             </tbody>         </table>     </div> </body> </html> <script type="text/javascript">     var uid = 1;     $(function () {         function contactscontroller($scope) {             $scope.contacts = [                 { id: 0, 'name': 'viral', 'email': 'hello@gmail.com', 'phone': '123-2343-44' }             ];           }     }); </script> 

if remove $(function () {}); method starts work , give required output. can me know details issue.

by wrapping controller $(function () { }); creating closure scope contactscontroller not visible outside scope, controller instead should have root (window) scope in order accessible! yo have remove closure or define controller window.contactscontroller = function(){} or contactscontroller = function(){} in order make "public"


Comments

Popular posts from this blog

basic authentication with http post params android -

vb.net - Virtual Keyboard commands -

css - Firefox for ubuntu renders wrong colors -