md-radio-button无法识别ng-checked

时间:2015-09-04 13:03:44

标签: javascript angularjs material-design

我正在尝试查看用户是否已订阅计划,如果是,请选中相应的单选按钮。

我有一组md单选按钮,我正在用ng-repeat创建:

          <md-radio-group class="md-primary" ng-model="plan">

        <div flex ng-repeat="planInfo in plans" class="plan-options">
          <md-radio-button value="{{planInfo._id}}" name="plan" class="md-primary" ng-checked="hasPlanID == planInfo._id" required>

            <h2 class="plan-name">{{planInfo.name}}</h2>

            <h3 class="plan-price">${{planInfo.price}}</h3>

          </md-radio-button>
        </div>

      </md-radio-group>

我有一个服务从数据库中获取所有计划:

      //check to see if user already has a plan
  MyService.GetPlan(currentUser.username).then(function (response) {
    if (response.success) {
      //This does resolve correctly
      $scope.hasPlanID = response.data.plan;

      MyService.GetAllPlans().then(function (response) {
        if (response.success) {
          //plans display properly
          $scope.plans = response.data;
        } else {
          $log.debug(response.message);
        }
      });
    } else {
      $log.debug(response.message);
    }
  });

注意HTML中的ng-checked属性。当我查看Chrome的检查器时,它会完全吐出您在代码中看到的内容,ng-checked =“hasPlanID == planInfo._id”。

如何让它识别变量值并将实际表达式解析为true或false?

3 个答案:

答案 0 :(得分:0)

您不必手动处理该检查。而不是填充$scope.hasPlanID属性,只需将$scope.plan设置为当前计划,如下所示:

//check to see if user already has a plan
MyService.GetPlan(currentUser.username).then(function (response) {
  if (response.success) {
    //This does resolve correctly
    $scope.plan = response.data.plan;

    MyService.GetAllPlans().then(function (response) {
      if (response.success) {
        //plans display properly
        $scope.plans = response.data;
      } else {
        $log.debug(response.message);
      }
    });
  } else {
    $log.debug(response.message);
  }
});

然后应该自动处理检查,因为您的模型设置为$scope.plan

<md-radio-group class="md-primary" ng-model="plan">

答案 1 :(得分:0)

$scope.sel = 'lu';
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<md-radio-group  ng-model="sel" ng-change="logos()" ng-model="myValue" class="settings-radio-button" layout="row">
    <md-radio-button value="lu" ng-checked="true" class="md-primary">Upload With URL</md-radio-button>
    <md-radio-button value="li" class="md-primary">Select Logo Image</md-radio-button>
</md-radio-group>

答案 2 :(得分:0)

如果您想选中SELECT f.[film name], f.city, MAX(CASE rt.[type name] WHEN 'director' THEN p.[participant name] ELSE null END) 'partecipant name', MAX(CASE rt.[type name] WHEN 'director' THEN 'director' ELSE null END) 'role type' FROM Films f JOIN FilmParticipants fp ON fp.filmID=f.filmID JOIN Participants p ON p.participantID=fp.participantID JOIN RoleType rt ON rt.roleID=fp.roleID WHERE f.city IN( SELECT f.city FROM Films f GROUP BY f.city HAVING COUNT(*)>1) ORDER BY f.city GROUP BY f.[Film Name], F.City 单选按钮,请使用 md-radio-button 而不是 ng-value ,而不是 value 例如

 ng-checked

此代码段将模拟计划的值,并根据 planInfo._id 的值选择相应的单选按钮。

希望这会有所帮助!