Skip to content

Instantly share code, notes, and snippets.

@kibernick
Created February 7, 2014 13:19
Show Gist options
  • Save kibernick/8862508 to your computer and use it in GitHub Desktop.
Save kibernick/8862508 to your computer and use it in GitHub Desktop.
Angular.js directive for manipulating data in the form of an array of strings (e.g. address lines, etc.)
myApp.directive("testing", [function() {
return {
restrict: "E",
templateUrl: "testing.html",
link: function(scope, element, attrs) {
scope.logIt = function() {
console.log(scope.someobj);
};
scope.addLine = function() {
scope.someobj.push("");
//if (scope.$root.$$phase != '$apply' && scope.$root.$$phase != '$digest') { scope.$apply(); };
};
scope.takeItOut = function(position) {
//if (scope.$root.$$phase != '$apply' && scope.$root.$$phase != '$digest') { scope.$apply(); };
console.log(scope.someobj)
console.log("taking out: " + position);
var tookOut = scope.someobj.splice(position, 1);
console.log(scope.someobj)
console.log("took out: " + tookOut);
};
},
scope: {
someobj: "="
}
}
}])
<ol>
<li ng-repeat='part in someobj track by $index'>
<input ng-model="someobj[$index]">
<button type="button" class="btn btn-danger btn-xs" ng-click="takeItOut($index)">x</button>
</li>
</ol>
<button type="button" ng-click="logIt()" class="btn btn-default">logIt</button>
<button type="button" ng-click="addLine()" class="btn btn-default">addLine</button>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment