$scope.con = {
"name": "tom",
"myname": "chenjy"
};
$scope.expr = "'my name is '+ con.name";
$scope.sayName = $parse($scope.expr)($scope)
$scope.$watch("expr", function(newValue, oldValue, context) {
if(newValue !== oldValue) {
$scope.sayName = $parse(newValue)(context)
}
});
}]);
</script>
</body>
</html>
other
监听enter按键事件
<!DOCTYPE html>
<html ng-app="App">
<head>
<meta charset="utf-8">
<script src="https://www.linuxidc.com/tools/libs/angular.js/1.7.8/angular.min.js"></script>
</head>
<body ng-controller="Ctrl">
<input type="text" ng-enter="enterEvent()">
<script>
angular.module("App", []).controller("Ctrl", ["$scope", "$parse", function($scope, $parse) {
$scope.enterEvent = function() {
console.log("Press Enter!");
};
}]).directive('ngEnter', ["$parse", function($parse) {
return function(scope, element, attrs) {
element.bind("keydown keypress", function(event) {
console.log(event);
if(event.which === 13) {
scope.$apply(function() {
scope.$eval(attrs.ngEnter);
/*$parse(attrs.ngEnter)(scope);*/
});
event.preventDefault();
}
});
};
}]);
</script>
</body>
</html>
在Ubuntu 18.04上安装Angular图文详解 https://www.linuxidc.com/Linux/2019-04/157972.htm
Linux公社的RSS地址:https://www.linuxidc.com/rssFeed.aspx