在AngularJS的bootstrap之后如何使用ngModel将输入控件与Javascript绑定?

时间:2014-03-18 04:51:30

标签: angularjs angular-ngmodel

如何在AngularJS的引导程序之后使用ngModel将输入控件与JavaScript绑定?

我尝试按ng-model方法添加setAttribute属性,但它不起作用!

    function FormController($scope) {
        $scope.name="OK";

        $scope.createBind=function(){
                    var texteditor=document.getElementById("test");
                    if(texteditor.getAttribute("ng-model")==null){
                        texteditor.setAttribute("ng-model","name");
                        $scope.$apply();
                    }
                }
    }

<input type="text" id="test">
<button ng-click="createBind()">Bind</button>

1 个答案:

答案 0 :(得分:0)

如果您通过javascript或jquery等制作它们,Angular不会得到更改...

您应该致电$scope.$apply()手动获取此类更改...

这是一个很好的article,让您了解$scope.apply() ...