AngularJS: access scope from bootstrap-ui accordio

2019-04-14 08:44发布

问题:

I'm trying to $watch for a change on a model within a ui-bootstrap accordion. Binding works within the view, but when the model changes, the $watch doesn't fire.

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

How do I get the value of $scope.myModel in the controller?

回答1:

Use an object rather than a primitive:

<div ng-controller="AccordionDemoCtrl">
  <accordion>
    <accordion-group heading="Static Header">
      <input ng-model="model.myModel"> {{ model.myModel }}
    </accordion-group>
  </accordion>
</div>

angular.module('plunker', ['ui.bootstrap']);
function AccordionDemoCtrl($scope) {
  $scope.model = {myModel: ''};
  $scope.$watch('model.myModel',function(){
    console.log($scope.model.myModel);
  })
}

Plunker