使用jQuery将ng-model添加到div

时间:2017-11-17 03:54:09

标签: javascript jquery html css angularjs

我从棱角开始,我遇到了问题。我有这个div。它包含div,它显示角度为{{stored}}值。当您在文本框中键入内容并单击按钮时。

<div>
  <div id="hidestore">{{stored}}</div> <input id="dobind" type="text" />
  <input type="button" id="showstore" value="show" />
</div>

它调用此代码,其中添加textbox一个ng-model,对应上面的{{stored}}。 基本上我希望div从按下按钮的时间开始显示{{stored}}值(不使用$scope)。

$("#showstore").click(function() {
  $("#dobind").attr("ng-model", "stored")
});

抱歉我的语法不好。我需要帮助!

2 个答案:

答案 0 :(得分:2)

在angularjs中使用jquery是一种不好的做法,但并不是说你无法实现你的目标。您需要对其进行预绑定,而不是在节目上绑定ng-model。只需隐藏hidestore div,然后在点击“显示”按钮后显示该按钮,这样就可以很好地angularjs angular.module('myApp',[]) .controller('myAppCtrl',function($scope){ $('#hidestore').css('display','none'); $("#showstore").click(function(){ $('#hidestore').css('display','block'); }); }); {/ 3}}。

&#13;
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
 <script data-require="jquery@*" data-semver="3.1.1" src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script>
<div ng-app="myApp" ng-controller="myAppCtrl">

      <div>
    <div id="hidestore">{{stored}}</div>
    <input id="dobind" type="text" ng-model="stored"/>
    <input type="button" id="showstore" value="show" />
  </div>        
    </div>
&#13;
CLLocationManager
&#13;
&#13;
&#13;

答案 1 :(得分:1)

将jquery与angularjs一起使用并不是一个好习惯。您需要为输入框创建一个s。和ng-click按钮功能。当您单击按钮时,将输入框文本分配给stored ng-model。

&#13;
&#13;
angular.module('sample',[])
.controller('sampleCnt',function($scope){
$scope.show = function(){
  $scope.stored = $scope.text;
}
});
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<div ng-app="sample" ng-controller="sampleCnt">

        <div id="hidestore" >{{stored}}</div> <input id="dobind" type="text" ng-model="text"/>
        <input type="button" id="showstore" ng-click="show()" value="show"/>
    </div>
    
    </div>
&#13;
&#13;
&#13;