AngularJS, how to bind a variable to concatenation of two other bound variables?

后端 未结 4 2265
情书的邮戳
情书的邮戳 2021-02-20 09:01

I am new to AngularJS and trying to build an AngularJS practice app, in which, the user will concatenate a url from multiple inputs, i.e. protocol, domain, path, param1, param2,

4条回答
  •  没有蜡笔的小新
    2021-02-20 09:12

    You shouldn't assign scope variables in the view (the HTML) - it's only for reading them.

    To make a new variable from inputs, add a ng-model to each of them and then in the controller define a method that makes a $scope variable from them e.g.

    Your HTML form:

    
    

    JS:

    function MyCtrl($scope) {
        $scope.urlParts = {};
        $scope.urlParts.protocol = ""; 
        $scope.urlParts.domain = ""; 
        // etc... 
        // These values will be updated when the user types in the input boxes
    
        $scope.makeUrl = function() {
          return $scope.urlParts.protocol + "://" + $scope.urlParts.domain + $scope.urlParts.path + "?" + $scope.urlParts.param1 + "&" + $scope.urlParts.param2 + "&" + $scope.urlParts.param3;
        }
    }
    

提交回复
热议问题