简体   繁体   中英

Angularjs controller name in application config vs ng-controller

I have a problem with scope variables.Here is the cases;

Case 1 : Specifying controller name in html with ng-controller tag.In this case alert prints input variable.

   <ion-view view-title=“TEST”>
      <ion-content ng-controller=‘TestController’>

       <input name="aaa" id="dsds" ng-model="aaa" type="text"/>
            <h3>{{aaa}}</h3>

              <button class="button" ng-click=“test()”>Test</button>

      </ion-content>
    </ion-view>

   //CONTROLLERS//
    $scope.test = function(sql){
        var x = $scope.aaa;
        alert(x);
    };

Case 2 : when I define controller in config , The following alerts “undefined”

state('app.test’, {
     url: ‘/test’,

     views: {
       'menuContent': {
      controller: 'TestController',
         templateUrl: 'templates/programs/test.html'
       }
     }
   })

<ion-view view-title=“TEST”>
  <ion-content>

   <input name="aaa" id="dsds" ng-model="aaa" type="text"/>
        <h3>{{aaa}}</h3>

          <button class="button" ng-click=“test()”>Test</button>

  </ion-content>
</ion-view>

   //CONTROLLERS//

$scope.test = function(sql){
    var x = $scope.aaa;
    alert(x);
};

In case 2, If I change ng-model =“aaa” with ng-model=“$parent.aaa” then alert gets the values.

So my question is what is the difference between giving controller name in application config section and ng-controller attribute

Thanks

This Situation only with Ionic. because Ionic is Angular Directive based Framework. All Directive have already self Controller So If you declare ng-model="aaa" then it's variable bind with self Controller $scope variable.

You see also Ionic on github and find .directive('ionView' you see inside directive already define a Controller. I think This is possible condition.

Ionic git hub

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM