I’m trying to use AngularJS to build some custom form components for a wordpress widget’s admin view, but I cant seem to get angular firing on all cylinders.
I’m including angular and my custom JS in my plugin as so:
wp_enqueue_script( 'angular', plugin_dir_url( __FILE__ ) . '/js/angular.min.js' );
wp_enqueue_script( 'featured_admin', plugin_dir_url( __FILE__ ) . '/js/admin.js' );
In the form
function of my WP_Widget
class, I’ve added the following markup:
<div ng-app='MyApp'>
<div class="my-form" ng-controller="FormController">
{{hello}}
</div>
</div>
And then in my /js/admin.js
file I have the following javascript:
var app = angular.module('MyApp', []);
app.controller('FormController', ['$scope', function ($scope) {
console.log('do something');
$scope.hello = "hello world";
}]);
When I run the above code in wordpress, I get ‘do something’ in the console, so I believe the module and controller are all hooked up correctly. However, the view is outputting {{hello}}
rather than the variable in the scope.
Has anybody had any experience with getting angular up and running in wordpress? I’m inclined to believe it has something to do with how wordpress renders it’s admin forms. Is there a way to defer the execution of the angular code, until I’m certain wordpress has rendered all of the forms?
Use manual boostrapping as an alternative: